在UITableView Cells

时间:2017-11-17 10:18:35

标签: ios objective-c uitableview uitableviewsectionheader

我一直在玩这个问题,因为我已经尝试了很多方法来解决这个问题,但还没有成功。我有一个包含3个自定义单元格的tableview,并且我已经为最后两个部分添加了section header。这是屏幕截图。 Header Gender is repeating

当我在TextView中输入文本时,

显示最后一个部分标题正在重复。我的TextView是可编辑的,我已禁用滚动以根据文本大小调整textview的大小。

这是代码。

    -(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委托方法。

-(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];
}

我试过这个#SO solution但没有帮助。任何帮助都会在这方面受到高度赞赏。提前谢谢。

1 个答案:

答案 0 :(得分:0)

根据我对这个问题的理解,你可以通过在" titleForHeaderInSection"中设置章节标题来解决它。就像在#34; cellForRowAtIndexPath"中设置单元格一样对于每个部分, 这样,您不设置标题的部分不会引起任何问题。

- (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;
}

    }