繁体   English   中英

iOS - 在带有静态单元格的UITableViewController上使用UISearchDisplayController

[英]iOS - Using a UISearchDisplayController on a UITableViewController with static cells

我在这个应用程序(使用Storyboard)的侧边栏上遇到了一些麻烦我正在努力。 侧边栏是一个UITableViewController ,我想在顶部有一个搜索栏,所以我将Search Bar and Search Display Controller对象放入Storyboard。 我在5个静态单元格中有侧边栏的内容,搜索栏搜索远程数据库以检索结果。

我的问题是,如果我的搜索结果包含超过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不允许我在原型单元格中拥有容器视图。 我想知道是否有任何选择我必须解决这个问题。

我设法在同事的帮助下解决了这个错误。 在更多地关注调用堆栈之后,我们意识到应用程序在数据源方法(索引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;
}

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

你可以覆盖:

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

并且在某些情况下返回自定义UITableViewCell在其他情况下调用super ,就像您已覆盖numberOfRowsInSection

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM