【问题标题】:UITableViewCell is nil when not visible in a large UITableView (Xcode, iOS)UITableViewCell 在大型 UITableView (Xcode, iOS) 中不可见时为 nil
【发布时间】:2015-09-26 16:17:36
【问题描述】:

我用 27 个静态 UITableViewCells 制作了一个大型 UITableView。选择UITableViewCell 时,应将其accessoryType 设置为UITableViewAccessoryCheckmark,并将最后选择的UITableViewCellaccessoryType 设置为UITableViewAccessoryNone

- (void)viewWillAppear:(BOOL)animated {
  [super viewWillAppear:animated];

  // get an NSInteger from NSUserDefaults

  [self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:savedInteger inSection:0] animated:NO scrollPosition:UITableViewScrollPositionTop];
  [self tableView:self.tableView didSelectRowAtIndexPath:[NSIndexPath indexPathForRow:savedInteger inSection:0]];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  // get the NSInteger again

  UITableViewCell *lastCell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:savedInteger inSection:0]];
  UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

  lastCell.accessoryType = UITableViewAccessoryNone;
  cell.accessoryType = UITableViewAccessoryCheckmark;

  // save indexPath.row in NSUserDefaults and do some other stuff

当您选择一个靠近最后选择的UITableViewCellUITableViewCell 但当最后选择的UITableViewCell 距离大约 11 行左右时,这工作正常,UITableViewCell *lastCellnil 和复选标记不会消失。

为什么会这样,我该如何解决?提前致谢。

【问题讨论】:

  • 不要显式调用didSelectRowAtIndexPath
  • 你应该在cellForRowAtIndexPath中设置一个单元格的accessoryType。
  • 在您的模型对象中有 1 个 bool,您可以从该 bool 处理附件视图。如果 bool 为真,则显示它,否则不要..

标签: ios objective-c uitableview accessorytype


【解决方案1】:
如果单元格不可见或索引路径超出范围,

cellForRowAtIndexPath 返回 nil
因此,检查当前选中的单元格是否可见。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    // get the NSInteger from NSUserDefaults
    NSIndexPath *previousSelection = [NSIndexPath indexPathForRow:savedInteger inSection:0];
    if ([tableView.indexPathsForVisibleRows containsObject:previousSelection]) {
        UITableViewCell *lastCell = [tableView cellForRowAtIndexPath:previousSelection];
        lastCell.accessoryType = UITableViewAccessoryNone;
    }

    // save indexPath.row in NSUserDefaults

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.accessoryType = UITableViewAccessoryCheckmark;

    // do other stuff
}

此外,为了确保首先使用所需的 accessoryType 创建表格视图单元格,请在 中设置表格视图单元格的 accessoryType >cellForRowAtIndexPath.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    // get the table view cell and set the details

    cell.accessoryType = UITableViewAccessoryNone;

    // get the NSInteger from NSUserDefaults
    if (indexPath.row == savedInteger) {
        cell.accessoryType = UITableViewAccessoryCheckmark; 
    }

    return cell;
}

【讨论】:

    【解决方案2】:

    cellForRowAtIndexPath 只返回可见单元格,因为 UITableView 重用单元格(当您从上到下滚动单元格时,它只初始化几个单元格,以此类推,效率很高)

    查看类似问题here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-24
      • 1970-01-01
      相关资源
      最近更新 更多