0

从一天开始我就一直在解决这个问题,我尝试了很多方法来解决这个问题,但还没有成功。我有一个带有 3 个自定义单元格的表格视图,并且我为最后两个部分添加了部分标题。这是屏幕截图。 标题性别重复

当我在 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解决方案,但没有帮助。在这个方向上的任何帮助将不胜感激。提前致谢。

4

1 回答 1

0

根据我对问题的理解,您可以通过在“titleForHeaderInSection”中设置部分标题来解决它,就像在“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;
}

    }
于 2017-11-17T10:45:08.863 回答