2011-10-06 79 views
1

我期待爲我的UISearchDisplayController定製UITableViewCellssearchResultsTableView是隻讀屬性。我有searchviewcontroller的委託和數據源返回自定義單元格,但是,它似乎仍在使用searchResultsTableView單元格。使用我自己的UITableViewCells獲得搜索結果的最佳方式是什麼?custom searchResultsTableView

回答

10

當搜索開始時,會創建一個全新的UITableView。這個新的表格視圖將使用默認設置並且不匹配您的表格視圖。在你UISearchDisplayDelegate,覆蓋searchDisplayControllerWillBeginSearch:爲了重新主題的新的UITableView,就像這樣:

- (void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller { 
    // Re-style the search controller's table view 
    UITableView *tableView = controller.searchResultsTableView; 
    tableView.backgroundColor = [UIColor blueColor]; 
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone; 
} 
+0

有沒有什麼辦法可以讓我使用UITableViewCells,我的委託/數據源返回?我不想只修改屬性。 – nmock

+0

爲搜索創建的tableView將(應該?)已經在使用您現有的委託/數據源。 –

0

我不知道我完全理解這個問題,但它聽起來像是你希望兩個不同類型的UITableViewCell(子類?)在您輸入搜索內容或不搜索時返回。是對的嗎?

像約翰說的那樣,並且根據您如何連接UISearchDisplayController,搜索結果表格視圖和默認表格視圖將激發cellForRowAtIndexPathwillDisplayCellForRowAtIndexPath。在這些方法中,您可以檢查哪個tableView要求單元格並根據需要進行自定義。我定製了我的細胞的背景,文字和顏色,這種方法對我來說效果很好。如果你願意,我可以發佈代碼。