2013-08-28 102 views
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"; 
} 
+0

什么开始发生了蓝色的 – rdelmar

+0

喜误差标题被重复使用的表格单元格没有索引路径。 表格视图在滚动时会出现不稳定的行为。直到所有的单元格被加载,但由于它们没有被重用,这是一个问题。 – CW0007007

+0

根本不清楚你遇到了什么问题。请在您的帖子中添加更多信息来解释您的问题。如果你的应用程序抛出一个异常,也包括这一点。 – Aaron

回答

1

我几天后工作了这一点。在我的自定义单元格我有一个TextView,当我将它添加到内容查看我这样做:

[self.cellTextView setClearsOnInsertion:YES]; 

这是问题的原因;任何人都有类似的问题。

快乐编码:-)

+2

任何人都可以解释为什么这会导致问题? –

+0

我怀疑这是与表视图单元出队。但我无法确定。 – CW0007007

-1

可以尝试以下的(前提是你没有在该indexpath细胞中的任何特定配置): -

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

还请后你做了什么错误如果上述不起作用。

也有看看下面的链接: -

What is the meaning of the "no index path for table cell being reused" message in iOS 6/7?

+0

相同的控制台日志。 – CW0007007

+0

编辑我的答案 – Max

+0

内部错误似乎不是一个好答案......不会与其他表格一起使用。 – CW0007007

7

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

A UITableViewCell被用作正常视图。因此它的'indexPath未设置。在我们的情况下,正在通过viewForHeaderInSection返回:

希望这会有所帮助。

克里斯

+3

那么你做了什么来解决它? – shim

+4

我停止使用UITableViewCell作为普通视图 - 即我需要创建一个普通的视图,我用UIView而不是UITableViewCell。 – chrisoneiota

59

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

编辑:这在某种程度上导致了事故对UI按键长按手势。为了解决这个问题,我放弃了使用单个项目的另一部分作为假部分标题。

+0

这解决了它对我来说。在我的情况下,它是viewForFooterInSection方法。谢谢! – GivP

+0

我可以确认,在此部分上长时间按下会导致应用程序崩溃 –

+1

但是,我最终所做的是将LongPressGesture添加到内容视图中,这会阻止将LongPress事件传递给已经解除分配的Cell正在返回内容视图,这是导致崩溃并解决问题的原因。 –

1

那么,我只是用了一天的好几天试图解决这个问题,所以希望这种替代解释可以节省一些人的一些时间。

我有一个tableview,它在加载的时候有时会给出这个消息。事实证明,这是由加载视图时触发核心数据对象的KVO通知引起的。 (在观察一个变化时,我的控制器试图在桌面视图上调用reloadData,通过在视图完成加载之前不观察对象来修复(之前,我通过访问器分配了对象之后开始观察对象)

TLDR:检查是否可能试图从主线程以外的地方重新加载数据。

更妙的是:

这是你会不会碰上,如果你(因为我发现你真的应该)使用儿童托管对象上下文,只有合并更改回对你的主要MOC有问题主线在明智的时候。

2

就我而言,此错误仅在iPhone(不是iPad)上提供,因为我已将[self.tableView beginUpdates];及更高版本[self.tableView endUpdates];放入[UIView transitionWithView:duration:options:animations:compeletion];之内。为了解决这个问题,我只是删除了开始/结束更新,因为我更喜欢淡入淡出的动画,而不是淡入淡出,加上动画细胞高度的变化。

0

我在tableView:viewForHeaderInSection中返回了UITableViewCell,但返回[cell contentView]导致单元格内的任何按钮崩溃,并且在单元格周围设置视图包装看起来很浪费。相反,我把我的UITableViewCell课改为UITableViewHeaderFooterView,它的功能就像一个魅力!

如果你得到这个问题:

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

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

1

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

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { 
    CustomerCellHeader *headerViewCell = [tableView dequeueReusableCellWithIdentifier:kCustomerCellHeaderIdentifier]; 
    headerView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth; 
    [headerView addSubview:headerViewCell]; 
    return headerView; 
} 
+0

初始化视图的代码在哪里? –

0

我有同样的问题。当我滚动并重用单元格时出现此消息。

回答了很多关于viewForHeaderInSection的问题,但我没有使用它们。如果有人有类似的问题,我特此表明我的解决方案。

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

所以我删除了观察者,当一个细胞是deinited。

看来问题解决了。我无法保证。

10

我在执行viewForHeaderInSection期间发现了此警告。

这是我在斯威夫特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 

这是斯威夫特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 
+1

此解决方案在Objective C中为我工作,谢谢:-)想要补充一点,我必须在我的标题单元格上将“TransTranslatesAutoresizingMaskIntoContraints”设置为“NO”,并将它限制到我的containerView。也为我的viewWillTransitionToSize旋转:withSizeCoordinator:我不得不添加一个调用,让我的表视图重新加载它的数据,然后在我的视图setNeedsLayout。希望这个信息也能帮助别人。 – CodenameDuchess

+0

谢谢你!!!! –

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 
    } 
0

我收到了同样的错误,但对于differen理由。

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

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

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(),而这正是给了我卑鄙错误... 我希望它永远不会帮助别人哈哈!(:?

编码快乐

相关问题