ios - 防止 UITableViewCells 重复内容

标签 ios objective-c uitableview

我有以下代码,它有 2 个部分。我希望有 3 个单元格的第一部分有文本,而第二部分没有文本。问题是在第 2 节中的第 6 个左右的单元格之后,单元格重复了第 1 节中的文本。

所以我的单元格看起来像:第 1 部分 - 个人资料、联系人、设置;第 2 部分 - 空白 空白、空白、空白、空白、配置文件、联系人、设置。

我究竟做错了什么?

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if(section == 0) return 3;
    else return 9;
}

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

    if(indexPath.section == 0) {
    switch (indexPath.row) {
        case 0:
            cell.textLabel.text = @"Profile";
            break;
        case 1:
            cell.textLabel.text = @"Contacts";
            break;
        case 2:
            cell.textLabel.text = @"Settings";
            break;

        default:
            break;
    }

    } else {
         //nothing should appear in these cells
    }

    return cell;
}

最佳答案

滚动时重复使用表格 View 单元格,即 dequeueReusableCellWithIdentifier可以返回以前用于显示不同行的单元格。

因此,您必须设置内容
明确地,即使在其他情况下:

} else {
    //nothing should appear in these cells
    cell.textLabel.text = @"";
}

关于ios - 防止 UITableViewCells 重复内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18911920/

相关文章:

android - iOS:具有扩展单元格的 TableView

iphone - 如何从第二层向下建立 UINavigationController(不是从 Delegate/RootController 层)

iphone - iPhone 单元测试中的 NSHomeDirectory

ios - 使用带数组的 NSDictionary 创建 JSON 文件

objective-c - 从 Plist 中存储和检索

ios - iAd 拒绝展示

iphone - 仅在为设备编译时变量 "undeclared"

objective-c - 在 Obj C 中初始化和修改 C 结构体数组

objective-c - 如何为 UIBezierPath 设置动画

ios - UITextField TableView 单元格 - iOS