【问题标题】:"no index path for table cell being reused" issue for custom cell自定义单元格的“没有重复使用表格单元格的索引路径”问题
【发布时间】:2013-06-17 04:55:01
【问题描述】:

我有一个表格视图,其中我有一个自定义单元格,其中我有另一个表格视图。在这个cellForRowAtIndexPath 方法中,我收到了这个错误no index path for table cell being reusedcell gets disappeared after scroll。这是 indexpath 问题还是 cellidentifier 问题?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"CustomCell";
CustomCell *customCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];     
//    __block CustomCell *customCell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
//    if (customCell == nil)
//    {
//        customCell = [[[CustomCell alloc] initWithFrame:CGRectMake(0, 0, 320, 416)] autorelease];
//   
// }
[customCell prepareCell:arrCategory];
return customCell;
}
-(void)prepareCell:(NSArray *)arrCategory
{
      if(mutArr != nil) {
        [mutArr removeAllObjects];
        mutArr = nil;
        [mutArr release];
 }
 mutArr = [[NSMutableArray alloc] arrCategory];
 [tblCusom reloadData];
}

我通过this SO ques 但我没有使用这个问题中使用的方法。那么我无法追踪它可能是什么问题。谷歌搜索后也没有发现同样的问题

【问题讨论】:

    标签: objective-c ios6 uitableview custom-cell


    【解决方案1】:

    如果我没看错的话,你需要检查委托的调用方法的tableView(mainTableView 或tblCustom)。 像这样的:

       - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
        {
            if (tableView == tblCusom)
            {
               __block CustomCell *categoryCell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
                if (customCell == nil)
                {
                    customCell = [[[CustomCell alloc] initWithFrame:CGRectMake(0, 0, 320, 416)] autorelease];
    
                }
    
                [customCell prepareCell:arrCategory];
            }
            else if (tableView == self.myMainTableView)
            {
                static NSString *CellIdentifier = @"CustomCell";
                CustomCell *categoryCell = [tableView   dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
                return categoryCell;
            }
    
        return customCell;
    }
    

    希望对你有帮助。

    【讨论】:

    • 我为所有人制作了不同的类。所以这段代码在那个类中,我正在调整我的内部单元格,其中包含表格视图
    • 好的,请显示您创建 tblCustom 的代码。它可能在 cellForRowAtIndexPath: 中,用于主 tableView。
    【解决方案2】:

    我也长期面临这个错误并找到了解决方案:-

    [tblCusom reloadData];
    

    替换为

    [self performSelector:@selector(tableReloadMethod) withObject:nil afterDelay:0.5];
    

    添加这个方法:-

    -(void)tableReloadMethod
    {
        [tblCusom reloadData];
    
    }
    

    希望对你有帮助。

    【讨论】:

    • 非常“hacky”的解决方案,如果您发现自己使用延迟来解决问题,请三思而后行。这个答案stackoverflow.com/a/12773104/250190 表明这是一个已知的内部错误。
    【解决方案3】:
        CustomCell *categoryCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    

    您正在创建一个单元格实例但没有在任何地方使用它,并且该单元格是从重用标识符方法返回的。所以实际上在您的代码中没有使用这一行

    【讨论】:

    • 这是一个错字..我正在声明 CustomCell 对象并在 preparecell 方法中使用它。更新了问题
    猜你喜欢
    • 2013-08-31
    • 1970-01-01
    • 2017-08-05
    • 1970-01-01
    • 2014-10-17
    • 1970-01-01
    • 2011-02-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多