尝试将同一索引路径的多个单元出队,这是不允许的

问题描述 投票:0回答:6

我有一个非常简单的 UITableView,自 iOS6 以来一直工作得很好......我最近尝试从 iOS10-iOS11 进行新的构建,现在我得到了这个新的 NSInternalConsistency 异常,我以前从未见过,它字面意思是新的 iOS 版本脱颖而出......

*** 由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“尝试出队” 同一索引路径有多个单元格,这是不允许的。如果你 确实需要出列比表视图请求的更多单元格, 使用 -dequeueReusableCellWithIdentifier: 方法(没有索引 小路)。单元格标识符:WorkOrderCell,索引路径:{length = 2, path = 0 - 0}'

我已经复习了很多教程,我觉得这段代码没有什么特别的错误。事实上,正常的网格加载工作正常,这是我的代码:

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

    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"WorkOrderCell" forIndexPath:indexPath];



    // Configure the cell...  do some work, return it



    return cell;
}

还有其他人遇到过这个问题吗?您采取了什么措施来解决它?

我应该更具体一点,正常的 UITableView 确实加载得很好,这个具体问题是我有一个 SearchBar,当用户输入搜索时,我正在过滤结果并显示结果网格:

//Start Search/Filter Code
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
    NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"Job contains[cd] %@ or Address contains[cd] %@ or WorkOrderId contains[cd] %@ or Supervisor contains[cd] %@", searchText, searchText, searchText, searchText];
    self.filteredWorkOrders = [self.workOrders filteredArrayUsingPredicate:resultPredicate];
}


-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
    [self filterContentForSearchText:searchString
                               scope:[[self.searchDisplayController.searchBar scopeButtonTitles]
                                      objectAtIndex:[self.searchDisplayController.searchBar
                                                     selectedScopeButtonIndex]]];

    return YES;
}

过滤器代码本身工作正常,只是它触发了 UITableView 委托,从而开始使单元格出队(搜索结果网格中应该为空??)

无论如何,我都需要一些帮助来解决这个问题......

ios objective-c uitableview
6个回答
12
投票

另一种可能性是,您可能不小心在同一个 tableView 中多次使用

dequeueReusableCell
方法。

例如,您也可能错误地在

didSelectRowAt
didEndDisplaying cell
中使用它。


11
投票

请勿在

self.tableView
方法(或任何其他数据源和委托方法)中使用
cellForRowAtIndexPath
。使用
tableView
参数。

当您的数据源和委托方法用于多个表视图时,这一点非常重要。


5
投票

正如警告消息所说,只需使用

dequeueReusableCell(withIdentifier: "YourIdentifierCellHere")

没有

for: indexPath

3
投票

在我的例子中,我在由于另一个并行进程而重新加载表时调用“cellForRowAt indexPath”。然后我实现了标志并延迟了“cellForRowAt indexPath”,同时“cellForRowAt indexPath”方法正在工作。所以它不会对同一个单元并行加载


1
投票

检查您是否多次启动同一个表格视图单元格。

在我的例子中,我启动了表格视图单元两次,如下所示:

let cell = tableView.dequeueReusableCell(withIdentifier: "SampleTableViewCell", for: indexPath) as! SampleTableViewCell
cell.label.text = "Testing"
If name != "" {
   let cell = tableView.dequeueReusableCell(withIdentifier: "SampleTableViewCell", for: indexPath) as! SampleTableViewCell
   cell.userName.text = name
}

然后我删除了第二个启动并更新了代码如下:

let cell = tableView.dequeueReusableCell(withIdentifier: "SampleTableViewCell", for: indexPath) as! SampleTableViewCell
cell.label.text = "Testing"
If name != "" {
   cell.userName.text = name
}

0
投票

我的问题是在

return
中缺少
cellForRowAt

我在 if 语句中缺少

return
,因此它尝试在第一个索引上创建两个不同的单元格:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    if indexPath.row == 0 {
        let cell = table.dequeueReusableCell(withIdentifier: "FirstIdentifier", for: indexPath) as! FirstCell
    }
    let cell = table.dequeueReusableCell(withIdentifier: "AnotherIdentifier", for: indexPath) as! AnotherCell
    return cell
}
© www.soinside.com 2019 - 2024. All rights reserved.