没有重用表单元格的索引路径

CW0*_*007 16 reusability tableview tableviewcell ios

这开始发生了.任何想法:代码:

CUSTOMCLASSNAME(我已经替换了实际的类名,因为它包含客户端的名称.)

初始化我的tableView:

[self.tableView registerClass:[CUSTOMCLASSNAME class] forCellReuseIdentifier:[self reuseIdentifier]];
Run Code Online (Sandbox Code Playgroud)

在行的单元格中:

嗨,标题正在控制台中打印.这是我的cellForRow:

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

    AVTCheckListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:[self reuseIdentifier] forIndexPath:indexPath];

    [self configureCell:cell atIndexPath:indexPath];

    return cell;
}

- (void)configureCell:(AVTCheckListTableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {
    ChecklistGroup *group = [self.checklistFetchedResultsController.fetchedObjects objectAtIndex:indexPath.section];

    ChecklistItem *item = [self getChecklistItemForIndexPath:indexPath];

    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    [[cell cellTextView] setAttributedText:[[item checked] boolValue] ? [[NSAttributedString alloc] initWithString:[item name] attributes:@{ NSStrikethroughStyleAttributeName : @(NSUnderlineStyleSingle) } ] : [[NSAttributedString alloc] initWithString:[item name]]];
    [[cell cellTextView] setUserInteractionEnabled:[[group isUserDefined] boolValue]];
    [[cell cellTextView] setTag:indexPath.row];
    [[cell cellTextView] setDelegate:self];
    [[cell tickImage] setHidden:![[item checked] boolValue]];
}

//Method that returns re-use:

- (NSString *) reuseIdentifier {
    return @"CheckListTableView";
}
Run Code Online (Sandbox Code Playgroud)

Tal*_*tle 59

返回[cell contentView],而不是celltableView:viewForHeaderInSection:

编辑:这在某种程度上导致了UI按钮上的长按手势崩溃.为了解决这个问题,我放弃并使用另一个部分作为假部分标题.


Ale*_*ano 10

我在viewForHeaderInSection实施过程中发现了这个警告.

这是我在Swift 2.x中的解决方案:

let headerView = self.tableView.dequeueReusableCellWithIdentifier("MyCell") as! MyCell
let containerView = UIView(frame:headerView.frame)
headerView.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]
headerView.myLabel.text = "section title 1"
containerView.addSubview(headerView)
return containerView
Run Code Online (Sandbox Code Playgroud)

这是Swift 3版本:

let headerView = self.tableView.dequeueReusableCell(withIdentifier: "MyCell") as! MyCell
let containerView = UIView(frame:headerView.frame)
headerView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
headerView.myLabel.text = "section title 1"
containerView.addSubview(headerView)
return containerView
Run Code Online (Sandbox Code Playgroud)


chr*_*ota 7

我刚刚找到了此错误消息的原因.

A UITableViewCell被用作普通视图.因此它indexPath没有设定.在我们的案例中,一个被归还viewForHeaderInSection:

希望这可以帮助.

克里斯

  • 我停止使用UITableViewCell作为普通视图 - 即我需要创建普通视图的地方我使用的是UIView而不是UITableViewCell. (4认同)
  • 那么你做了什么修复呢? (3认同)

CW0*_*007 1

几天后我解决了这个问题。在我的自定义单元格中,我有一个 textView,当我将其添加到 contentView 时,我正在执行以下操作:

[self.cellTextView setClearsOnInsertion:YES];
Run Code Online (Sandbox Code Playgroud)

这就是问题的原因;如果其他人也有类似的问题。

快乐编码:-)

  • 谁能解释为什么这会导致问题? (2认同)