3

我在使用这个应用程序(使用Storyboard)的侧边栏时遇到了一些问题。侧边栏是一个UITableViewController,我希望顶部有一个搜索栏,所以我把Search Bar and Search Display Controller对象放入了Storyboard。我有5个静态单元格中的边栏内容,搜索栏搜索远程数据库以检索结果。iOS - 在具有静态单元格的UITableViewController上使用UISearchDisplayController

我的问题是,如果我的搜索结果中包含超过5元,我得到以下错误:

*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 5 beyond bounds [0 .. 4]' 

我不能完全肯定发生了什么幕后,但我相当肯定,尽管具有以下代码,但为Storyboard(5)中的表视图部分设置的行数覆盖了所有内容。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    if (tableView == self.searchDisplayController.searchResultsTableView) { 
     return [[self filteredCappegoryArray] count]; 
    } else { 
     return [super tableView:tableView numberOfRowsInSection:0]; 
    } 
} 

我会切换侧边栏使用动态细胞,但我的单元格包含一个容器视图和的XCode不会让我有在原型细胞的容器视图。我想知道是否有任何选择我必须解决此问题。

回答

2

我设法解决的帮助错误一个同事。在更多关注调用堆栈之后,我们意识到应用程序在数据源方法(索引3)中崩溃。

*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 5 beyond bounds [0 .. 4]' 
*** First throw call stack: 
(
0 CoreFoundation      0x000000010188c795 __exceptionPreprocess + 165 
1 libobjc.A.dylib      0x00000001015ef991 objc_exception_throw + 43 
2 CoreFoundation      0x000000010184502f -[__NSArrayI objectAtIndex:] + 175 
3 UIKit        0x00000001006d97df -[UITableViewDataSource tableView:indentationLevelForRowAtIndexPath:] + 115 
4 UIKit        0x0000000100318a08 __53-[UITableView _configureCellForDisplay:forIndexPath:]_block_invoke + 1574 
5 UIKit        0x00000001002a60ac +[UIView(Animation) performWithoutAnimation:] + 70 
[...] 

所以,我加了这个,现在结果显示完美无缺。

- (NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    return 0; 
} 

这实际上解决了两个问题,因为我注意到我的搜索结果在添加该方法之前也有不同的缩进。

+0

嗨真的很高兴你发布了这个'indentationLevelForRowAtIndexPath'修复程序。我一直在调试几个小时。 –

+0

非常感谢您为此发布解决方案。我遇到了同样的问题,并且我花了几个小时调试问题。再次感谢! – Guerrix

0

您可以覆盖:

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

而且在某些情况下,返回自定义UITableViewCell,并呼吁在其他国家,super,就像你重写numberOfRowsInSection

+0

是的,我已经这么做了。单元格全部显示正确,唯一的问题是表格视图拒绝允许比故事板设置中设置的数量更多的单元格。 – kevin

相关问题