16

这开始突然发生。任何想法:代码:

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

初始化我的 tableView:

[self.tableView registerClass:[CUSTOMCLASSNAME class] forCellReuseIdentifier:[self reuseIdentifier]];

在行的单元格中:

嗨,标题正在控制台中打印。这是我的 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";
}
4

13 回答 13

59

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

编辑:这以某种方式导致 UI 按钮上的长按手势崩溃。为了解决这个问题,我放弃并使用另一个带有单个项目的部分作为假的部分标题。

于 2014-01-23T20:57:59.683 回答
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

这是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
于 2015-11-02T11:13:48.400 回答
7

我刚刚找到了导致此错误消息的原因。

AUITableViewCell被用作普通视图。因此它的'indexPath没有设置。在我们的例子中,一个被退回viewForHeaderInSection

希望这可以帮助。

克里斯

于 2013-09-26T16:01:02.043 回答
2

[self.tableView beginUpdates];就我而言,此错误仅出现[self.tableView endUpdates];在iPhone(而不是 iPad)上,因为我已将[UIView transitionWithView:duration:options:animations:compeletion];. 为了解决这个问题,我刚刚删除了开始/结束更新,因为我更喜欢渐变动画而不是渐变加上动画单元格高度变化。

于 2014-08-08T18:46:30.593 回答
1

几天后我解决了这个问题。在我的自定义单元格中,我有一个 textView,当我将它添加到 contentView 时,我正在这样做:

[self.cellTextView setClearsOnInsertion:YES];

这就是问题的原因;以防其他人有类似的问题。

快乐编码:-)

于 2013-09-03T09:53:31.317 回答
1

好吧,我只是利用一天中的大部分时间试图弄清楚这一点,所以希望这种替代解释可以为其他人节省一些时间。

我有一个表格视图,有时在加载时会发出此消息。事实证明,这是由在加载视图时触发 Core Data 对象的 KVO 通知引起的。(在观察到变化时,我的控制器尝试在有问题的 tableview 上调用 reloadData。通过在视图完成加载之前不观察对象来修复(以前,一旦通过访问器分配对象,我就开始观察对象)

TLDR:检查您是否正在尝试从主线程以外的其他地方重新加载数据。

更好:

如果您(我发现您确实应该)使用子托管对象上下文并且仅在合理的时间将更改合并回主线程上的主 MOC,那么您就不会遇到这个问题。

于 2014-04-09T11:17:55.600 回答
1

如果您使用单元格作为标题视图部分标题,则必须将其放入容器中,然后返回容器视图以防止“表单元格没有索引路径被重用”。示例代码如下。

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    CustomerCellHeader *headerViewCell = [tableView dequeueReusableCellWithIdentifier:kCustomerCellHeaderIdentifier];
    headerView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
    [headerView addSubview:headerViewCell];
    return headerView;
}
于 2015-05-11T04:47:20.677 回答
0

我在返回UITableViewCellin时遇到了问题tableView:viewForHeaderInSection,但是返回[cell contentView]会导致单元格内的任何按钮崩溃,并且在单元格周围设置视图包装器似乎很浪费。相反,我将我的UITableViewCell课程改为 a UITableViewHeaderFooterView,它就像一个魅力!

如果您遇到此问题:

在 UITableViewHeaderFooterView 上设置背景颜色已被弃用。请改用 contentView.backgroundColor。

请记住将笔尖上的背景颜色更改为默认值。

于 2014-12-15T01:54:45.403 回答
0

我遇到过同样的问题。当我滚动并重复使用单元格时会出现此消息。

很多答案都谈到了 viewForHeaderInSection 但我没有使用它们。如果有人有类似的问题,我特此展示我的解决方案。

我在每个单元格内的文本字段中添加了观察者。我想知道这是否是导致此问题的观察者,因为在回收单元格时我没有删除观察者。

因此,当一个单元格被定义时,我删除了观察者。

看来问题已经解决了。我不能保证。

于 2015-07-10T00:12:19.593 回答
0

它在 swift 2.1 中帮助了我

  func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView {
            let headerCell:SBCollapsableTableViewHeader = self.friendsInfoTableView.dequeueReusableCellWithIdentifier("HeaderCell") as! SBCollapsableTableViewHeader
            let containerView = UIView(frame:headerCell.frame)
            headerCell.delegate = self
            headerCell.titleLabel.font = UIFont.boldSystemFontOfSize(15)
            headerCell.titleLabel.text = "\(self.ObjectArray[section].sectioName)"
            headerCell.collapsedIndicatorLabel.text = collapsed ? "+" : "-"
            headerCell.tag = section
            headerCell.backgroundColor =  UIColor(red:72/255,green:141/255,blue:200/255,alpha:0.9)
            containerView.addSubview(headerCell)
            return containerView
    }
于 2016-01-02T06:58:16.197 回答
0

我收到了同样的错误,但出于不同的原因。

我有一个自定义 UITableViewCell 将其中一个 UITextField 设置为成为FirstResponder。返回此自定义单元格的多个实例时发生错误。

我只是将自定义单元格的第一个实例设置为 becomeFirstResponder 并解决了问题。

于 2016-05-08T19:28:29.117 回答
0

就我而言(非常愚蠢的一个),我使用了这样的if let语法cellForRowAt

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    if let cell = tableView.dequeueReusableCell(withIdentifier: "carCell", for:
    indexPath) as? carTableViewCell {
        // Did some stuff here
        return CarTableViewCell()
    else {
        return CarTableViewCell()    
    }
}

如您所见,我返回了我通用的 carTableViewCell(),这就是给我这个卑鄙的错误的原因......我希望它会帮助某人哈哈(:

快乐编码!

于 2017-07-30T20:12:09.410 回答
-1

您可以尝试以下操作吗(前提是您在该索引路径中没有任何特定单元格的配置):-

CustomClassName *cell=[tableView dequeueReusableCellWithIdentifier:[self reuseIdentifier]];
if(cell==nil)
{
cell=[[CustomClassName alloc] initWithStyle:whatever_style reuseIdentifer:[self reuseIdentifier]];
}

如果上述方法不起作用,还请发布您遇到的错误。

另请查看以下链接:-

iOS 6/7 中“没有重复使用表格单元的索引路径”消息是什么意思?

于 2013-08-28T14:49:10.330 回答