20

我有一個UIViewController安裝在一個tableviewUISearchDisplayController故事板。UISearchDisplayController和UITableView原型單元崩潰

我想從self.tableview(它連接到故事板中的主桌面視圖)使用自定義原型單元格。它工作正常,如果self.tableview已返回至少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出列原型單元格,但我的猜測是沒有在self.tableview中創建,所以這是原因?

+0

你可以發佈'configureCell:atIndexPath:'方法的代碼嗎?問題可能出在 –

+0

之前,它崩潰之前,它會到達那裏......我將添加它雖然 – mootymoots

+0

上面添加...並沒有真正做很多 – mootymoots

回答

-1

如果在方法celForRowAtIndexPath中使用àUISearchDisplayController,則必須使用tableView參數方法,而不是控制器的保留指針。

在viewDidLoad中:

與此代碼

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

CustomSearchCell標識符不存在UISearchDisplayController單元格上。它是故事板中原始桌面視圖上的原型單元格。你不能添加一個原型單元格到故事板中的UISearchDisplayController ... AFAIK – mootymoots

7

我解決了這個通過複製原型細胞進入一個新的廈門國際銀行試圖

[self.searchDisplayController.searchResultsTableView registerNib:[UINib nibWithNibName:@"CustomSearchCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"CustomSearchCell"]; 

和最新的cellForRowAtIndexPath使用的方法的實現代碼如下不是原來的self.tableview:

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

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

除了擁有主表之外,UISearchDisplayController還管理它自己的UITableView(過濾表)。已過濾表格中的單元格標識符與主表格不匹配。你也想通過indexPath不能獲取細胞既表可以彼此截然不同的相對於行數等

所以不是這樣:

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

,而不是這樣做:

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

我在這上面掙扎了2個小時。你的回答很有幫助,尤其是你的注意,你省略了forIndexPath,因爲這些表可能有很大的不同索引。另外,對於其他讀者:確保你有self.tableview不只是tableview。 – ThinkBonobo

+0

太棒了。創造了我的一天。 –

+1

願上帝保佑你 – shannoga

0

這是一個老話題,但如果有人跑進了同樣的問題是,對我來說這是涉及具有在viewDidLoad中

self.tableView.estimatedRowHeight = 80; 
這條線

,並在執行具有可變高度的heightForRowAtIndexPath委託方法在不同條件下

if (indexPath.row == 0) { 
    return 44 + cellTopSpacing; 
} else { 
    return 44; 
} 

去除估計的行高度的同時解決它。