1

我正在使用UISearchDisplayController能夠顯示基於我從服務器檢索到的一些數據的自定義單元格的表。自定義UITableViewCell中的屬性沒有得到初始化的UISearchDisplayController的表

首先,我在我的UIViewController中設置了UISearchDisplayController。

self.searchController = [[UISearchDisplayController alloc] 
          initWithSearchBar:self.mySearchBar contentsController:self]; 
     self.searchController.delegate = self; 
     self.searchController.searchResultsDataSource = self; 
     self.searchController.searchResultsDelegate = self; 

我UIViewController中也實現了UISearchBarDelegate,這樣我就可以判斷一個搜索開始時。我成立了一個塊,所以當我的API調用返回它被調用和結果的字典被保存在self.searchResults屬性:

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar 
{ 
    // here we make the api call 
    [api getSomeInfo:searchBar.text complete:^(NSDictionary *json) { 

     self.searchResults = json; 
     [self.searchController.searchResultsTableView reloadData]; 
    }]; 
} 

現在,我的問題是,在我的UITableViewDataSource方法,我在那裏返回自定義單元格。我的手機被實例化,但它IBOutlets永遠不會被初始化,所以我不能設置自己的內容(文字,圖像等)正確:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (tableView == self.searchController.searchResultsTableView) { 

     cell = [tableView dequeueReusableCellWithIdentifier:@"SearchResultsCellIndentifier"]; 

     if (cell == nil) { 
      cell = [[SearchResultsCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; 
     } 

     cell.customLabel.text = [self.searchResults objectForKey:@"customText"]; // cell exists but cell.customLabel is nil!! 
    } 

} 

爲什麼是內容爲零?在我的Custom Cell類中有什麼地方需要設置內容?

謝謝!

+0

我想指出我使用Interface Builder來創建自定義單元格。它是在UIViewController的原型單元格下,它有它的標識符@「SearchResultsCellIndentifier」 – 2012-02-16 18:09:31

+0

在這裏找到答案:http://bit.ly/y4XXen 問題是當你試圖出隊一個可重用的單元格,並沒有得到任何回報。然後我嘗試創建一個新的單元格。 我實際上不得不從UIViewController的tableView出列可重用的單元格。 – 2012-02-16 19:41:59

回答

1

我認爲你的問題是你在創建單元格時使用了變量cellIdentifier,而在出列時使用了字符串常量。

總是重新創建一個單元格可以工作,但是效率不高,會導致嚴重的內存泄漏。

您應該首先根據您所在的表視圖以及您需要哪種類型的單元設置cellIdentifier,然後使用該cellIdentifier出列,然後根據需要創建一個新的單元標識符。

相關問題