UISearchDisplayController和UITableView原型单元崩溃 [英] UISearchDisplayController and UITableView prototype cell crash
问题描述
我在故事板中设置了 UIViewController
,其中包含 tableview
和 UISearchDisplayController
。
I have a UIViewController
setup in a storyboard with a tableview
and UISearchDisplayController
.
我正在尝试使用self.tableview中的自定义原型单元格(连接到故事板中的主表视图)。如果 self.tableview
在我加载视图时返回了至少1个单元格,但是如果 self.tableview
不加载单元格(因为没有数据),我加载 UISearchBar
并搜索 cellForRowAtIndexPath:
方法崩溃:
I am trying to use a custom prototype cell from self.tableview (which is connected to main tableview in the storyboard). It works fine if self.tableview
had returned at least 1 cell when I load my view, but if self.tableview
doesn't load a cell (as there is no data), and I load up the UISearchBar
and search, the cellForRowAtIndexPath:
method crashes:
- (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
行崩溃。
My fetchedResultsController seems to have data (1 section, 2 rows) at the point the above method is called. It crashes on the dequeueReusableCellWithIdentifier
line.
任何指针/想法?它应该从 self.tableview
中取出原型单元格,但我的猜测是在 self.tableview
中没有创建这是原因?
Any pointers/ideas? It should dequeue the prototype cell from self.tableview
, but my guess is there was none created in self.tableview
so this is the cause?
推荐答案
除了拥有主表外,UISearchDisplayController还管理它自己的UITableView(过滤表)。筛选表中的单元格标识符与主表不匹配。您还希望不是通过indexPath获取单元格,因为两个表在行数等方面可能会有很大的不同。
UISearchDisplayController manages it's own UITableView (filtered table), in addition to having your primary table. The cell identifier in the filtered table doesn't match your primary table. You also want to fetch the cell not by indexPath as both tables can be vastly different from each other with respect to number of rows, etc.
所以不要这样做:
UITableViewCell *cell =
[self.tableView dequeueReusableCellWithIdentifier:CellIdentifier
forIndexPath:indexPath];
改为:
UITableViewCell *cell =
[self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
这篇关于UISearchDisplayController和UITableView原型单元崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!