0

I have been playing around with this problem since a day and I have tried many ways to resolve this issue but have not yet succeeded. I have a tableview with 3 custom cells and I have added section header for last two sections. Here is the screen shot. Header Gender is repeating

which shows last section header is repeating when I enter text in the TextView. My TextView is editable and I have disabled the scrolling to adjust the size of the textview as per the text size.

Here is the code.

    -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
        return 3;
    }

    -(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
    {
        NSArray *titleArray = @[@"",@"About You",@"Gender"];

        NSString *titleHeading = [titleArray objectAtIndex:section];

        return titleHeading;
    }

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return section == 0 ? CGFLOAT_MIN : 35;
}

    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {

        NSString *identifier = [NSString stringWithFormat:@"cell%ld,%ld",indexPath.section, indexPath.row];
        id cell = [tableView dequeueReusableCellWithIdentifier:identifier];

if (indexPath.section == 1)
            {
                ProfileAboutCell *cellObj = [tableView dequeueReusableCellWithIdentifier:identifier];
                if (!cellObj)
                {
                    [_tableView registerNib:[UINib nibWithNibName:@"ProfileAboutCell" bundle:nil] forCellReuseIdentifier:identifier];
                    cellObj = [tableView dequeueReusableCellWithIdentifier:identifier];
                }
                cellObj.selectionStyle = UITableViewCellSelectionStyleNone;

                //[cellObj.txtAboutYou layoutIfNeeded];
        cellObj.txtAboutYou.delegate = self;
        cellObj.lbl = [[UILabel alloc] initWithFrame:CGRectMake(5.0, 0.0,cellObj.txtAboutYou.frame.size.width - 10.0, 34.0)];
        cellObj.lbl.text = @"About You";
        [cellObj.lbl setBackgroundColor:[UIColor clearColor]];
        [cellObj.lbl setTextColor:[UIColor lightGrayColor]];
        [cellObj.txtAboutYou addSubview:cellObj.lbl];
    //        [cellObj.txtAboutYou setText:[kUserDefaults valueForKey:kAbout]];
        //[cellObj.txtAboutYou sizeToFit];



                cell = cellObj;
            }

        return cell;
        }

TextView Delegate Method.

-(void)textViewDidChange:(UITextView *)textView
{
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:1];
    ProfileAboutCell *cell = [_tableView cellForRowAtIndexPath:indexPath];

    if(![textView hasText]) {
        cell.lbl.hidden = NO;
    }
    else{
        cell.lbl.hidden = YES;
    }
    NSInteger len = textView.text.length;
    cell.lblChar.text=[NSString stringWithFormat:@"%li",500-len];

    [_tableView beginUpdates];
    [_tableView endUpdates];
}

I have tried this #SO solution but no help. Any help would be much appreciated in this direction. Thanks in advance.

iHarshil
  • 739
  • 10
  • 22

1 Answers1

0

From my understanding of the issue you can solve it by set the sections headers in "titleForHeaderInSection" the same way you set the cells in "cellForRowAtIndexPath" for each section, this way sections that you don't set Headers for will not cause any issue.

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    NSArray *titleArray = @[@"",@"About You",@"Gender"];



if (indexPath.section == 1) {
        NSString *titleHeading = [titleArray objectAtIndex:1];
        return titleHeading;
}
 if (indexPath.section == 2) {
        NSString *titleHeading = [titleArray objectAtIndex:2];

        return titleHeading;
}

    }
Omar Masri
  • 349
  • 1
  • 8