gpt4 book ai didi

iphone - UISearchDisplayController 和 UITableView 原型(prototype)单元格崩溃

转载 作者:IT王子 更新时间:2023-10-29 08:04:26 26 4
gpt4 key购买 nike

我在 Storyboard中设置了一个 UIViewController,其中包含一个 tableviewUISearchDisplayController

我正在尝试使用来自 self.tableview 的自定义原型(prototype)单元格(它连接到 Storyboard中的主 tableview)。如果 self.tableview 在我加载我的 View 时返回了至少 1 个单元格,它工作正常,但是如果 self.tableview 没有加载单元格(因为没有数据),我加载了 UISearchBar 并搜索,cellForRowAtIndexPath: 方法崩溃了:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CustomSearchCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"CustomSearchCell" forIndexPath:indexPath];

[self configureCell:cell atIndexPath:indexPath];
return cell;
}

-(void)configureCell:(CustomSearchCell *)cell atIndexPath:(NSIndexPath *)indexPath {
User *user = [self.fetchedResultsController objectAtIndexPath:indexPath];

cell.nameLabel.text = user.username;
}

错误:

*** Assertion failure in -[UITableViewRowData rectForRow:inSection:heightCanBeGuessed:]
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'request for rect at invalid index path (<NSIndexPath: 0x9ef3d00> {length = 2, path = 0 - 0})

在调用上述方法时,我的 fetchedResultsController 似乎有数据(1 个部分,2 行)。它在 dequeueReusableCellWithIdentifier 行崩溃。

任何指示/想法?它应该使 self.tableview 中的原型(prototype)单元格出队,但我猜测 self.tableview 中没有创建原型(prototype)单元格,所以这是原因吗?

最佳答案

UISearchDisplayController 管理它自己的 UITableView(过滤表),除了拥有您的主表。筛选表中的单元格标识符与您的主表不匹配。您还希望不通过 indexPath 获取单元格,因为两个表在行数等方面可能大相径庭。

所以不要这样做:

UITableViewCell *cell =
[self.tableView dequeueReusableCellWithIdentifier:CellIdentifier
forIndexPath:indexPath];

改为这样做:

UITableViewCell *cell =
[self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];

关于iphone - UISearchDisplayController 和 UITableView 原型(prototype)单元格崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18560300/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com