iphone - UITableview 混淆单元格

标签 iphone ios uitableview

我的 UITableview 有问题。滚动或按下开关后,它有时会开始混淆单元格。因此,您可以让一个单元格的文本显示在另一个单元格中。我阅读了很多有关 tableViews 的内容,但没有找到适合我的代码的内容。 在这里:

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

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];

}

    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            cell.textLabel.text = NSLocalizedString(@"Course:",@"Course Section");
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            cell.detailTextLabel.text = entryTableCourseName;

        }

       else if (indexPath.row == 1) {
            cell.textLabel.text = @"Due Date:";
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            cell.detailTextLabel.text = entryTableDueDateString;            }
    }

    else if (indexPath.section == 1) {
        if (indexPath.row == 0) {
            //cell.textLabel.text = NSLocalizedString(@"Title", @"entryTableTaskTitle");
            [cell.contentView addSubview:nameField];

        }

        else if (indexPath.row == 1) {
            [cell.contentView addSubview:TaskView];
        }

    }


    else if (indexPath.section == 2) {

        if (entryTableGradeSwitch.on) {
            if (indexPath.row == 0) {
                cell.textLabel.text = NSLocalizedString(@"Grading", @"GradingCell");
                cell.accessoryView = entryTableGradeSwitch;
            }
            else if (indexPath.row == 1) {
                cell.textLabel.text = NSLocalizedString(@"Options", @"GradingCellOptions");
            }
        }
        else {
            cell.textLabel.text = NSLocalizedString(@"Grading", @"GradingCell");
            cell.accessoryView = entryTableGradeSwitch;
        }
    }

}

我觉得我在设置单元格时做错了什么。

最佳答案

这全都归功于单元格的可重用性。 StackOverflow 上有很多关于此的帖子,我建议您搜索“UITableViewCell Reusability Problems”,您会找到解决方案。

简而言之 .. 这里发生的事情是,当您滚动时,单元格基本上会重新使用之前在该 indexPath 中使用的单元格,并且 cellForRowAtIndexPath 会在该索引路径中分配一个不同的单元格。

解决方案 ?

  1. 为每个单元格使用不同的重用标识符。
  2. 使用困惑的 View 或标签对 tableViewCell 进行子类化。

关于iphone - UITableview 混淆单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10918940/

相关文章:

ios - PageControl 将标记放置在我想要的位置 ios 9

iphone - GameCenter - 登出玩家

iphone - 当 View Controller 呈现在另一个 View Controller 上时如何获取屏幕截图?

objective-c - 将 map 居中到可见区域

iphone - uitableview 的最后一行中的值发生变化

android - 实现数字滤波器 - 通过卷积或差分方程?

iphone - 什么是 NSIndexPath

ios - 视频播放GPUImage和swift

ios - SwiftUI - 创建多平台应用程序时,如何更改场景中的 View ?

ios - 窥视 iOS 9 应用程序崩溃