UITableView - 滚动太快时,最后一个单元格的内容替换第一个单元格

Bra*_*rad 5 objective-c uitableview ios

这可能是设计不佳造成的,但是当我滚动表格太快,然后滚动回顶部时,放置在最后一个表格单元格中的视图也放置在 tableView 中的第一个表格单元格上。

我觉得可能是我用if语句把一些静态内容放在第一部分,动态内容放在第二部分造成的。

任何帮助表示赞赏。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    // Configure the cell...

    if (indexPath.section == 0) {

        if (indexPath.row == 0) {

            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            UITextField *textField = [[UITextField alloc]initWithFrame:CGRectMake(10, 13, 282, 20)];
            textField.clearsOnBeginEditing = NO;
            textField.placeholder = @"enter template name";
            textField.font = [UIFont systemFontOfSize:15.0];
            textField.clearButtonMode = UITextFieldViewModeWhileEditing;
            textField.delegate = self;
            textField.text = [selectedTemplate name];
            [textField addTarget:self action:@selector(nameDidChange:) forControlEvents:UIControlEventEditingChanged];
            [cell.contentView addSubview:textField];

        } else {

            cell.textLabel.text =  @"Add a new question";
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

        }

    } else {

        NSString *label = [[sortedQuestions valueForKey:@"questionText"] objectAtIndex:indexPath.row];
        CGSize stringSize = [label sizeWithFont:[UIFont boldSystemFontOfSize:15] constrainedToSize:CGSizeMake(230, 9999) lineBreakMode:NSLineBreakByWordWrapping];

        UITextView *textV=[[UITextView alloc] initWithFrame:CGRectMake(5, 5, 230, stringSize.height+10)];
        textV.font = [UIFont systemFontOfSize:15.0];
        textV.text=label;
        textV.textColor=[UIColor blackColor];
        textV.editable = NO;
        textV.userInteractionEnabled = NO;
        textV.backgroundColor = [UIColor colorWithRed:0.97f green:0.97f blue:0.97f alpha:1.00f];
        [cell.contentView addSubview:textV];

        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    }

    return cell;
}
Run Code Online (Sandbox Code Playgroud)

Bra*_*rad 5

这是我根据 Wienke 的建议最终做的事情。我在我的故事板中添加了 3 个原型单元格,名为 Cell,Cell2,Cell3。

相关代码:

static NSString *CellIdentifier = @"Cell";
static NSString *CellIdentifier2 = @"Cell2";
static NSString *CellIdentifier3 = @"Cell3";

UITableViewCell *cell;

if (indexPath.section == 0 && indexPath.row == 0) {

    cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

} else if (indexPath.section == 0 && indexPath.row == 1) {

    cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier2 forIndexPath:indexPath];

} else if (indexPath.section == 1) {

    cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier3 forIndexPath:indexPath];

}
Run Code Online (Sandbox Code Playgroud)

我还添加了它来检查我的动态单元格并删除任何可能在添加新子视图之前徘徊的子视图。

    if ([cell.contentView subviews]){
        for (UIView *subview in [cell.contentView subviews]) {
            [subview removeFromSuperview];
        }
    }
Run Code Online (Sandbox Code Playgroud)


Wie*_*nke 1

它看起来像是为第 0 行 0 布局的单元格正在出列并用于例如第 0 行 1 行,这不会替换在保存第 0 节时为其所做的所有设置第 0 行材料。

您可能需要考虑使用 3 个单独的单元格标识符,一个用于第 0 部分第 0 行,一个用于第 0 部分第 1 行,一个用于其余所有单元格标识符。您将需要一组初步的 if 语句(或 switch-case 语句),以便在调用dequeueReusableCellWithIdentifier:.