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

相關問題