2012-07-05 95 views
1

我在我的主tableView中,有一些自定義單元格(基本上是帶有imageView的單元格)。重新加載searchResultsTableView

imageView僅當我的plist上的值爲false時才設置。然後,當它是真的,imageView是零。

基本上當用戶輸入detailView時,該值設置爲YES,而cell.imageView爲零。

而且它好,它的工作原理

我使用的是searchDisplayController,當我搜索的東西,有一個cell.imageView,去到的DetailView然後回來的searchResultsTable,電池仍具有的圖像,雖然它不應該,但主tableView有正確的單元格(所以,沒有圖像)。

我認爲這可能取決於searchResultsTableView,但我不確定。 我試過

[self.searchDisplayController.searchResultsTableView reloadData]; 

沒有效果。

我怎麼能重新加載searchResultsTableView,以便它顯示正確的單元格,那些與圖像和那些沒有圖像了?

任何幫助表示讚賞!

編輯

這是我cellForRowAtIndexPath方法:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
    } 


    NSArray *rows; 
    if (tableView == self.searchDisplayController.searchResultsTableView) { 
     rows = filteredList; //for search 
    } else { 
     NSDictionary *section = [localSortedTips objectAtIndex:indexPath.section]; 
     rows = [section objectForKey:@"Rows"]; 
    } 

    NSDictionary *item = [rows objectAtIndex:indexPath.row]; 
    cell.textLabel.text = [item objectForKey:@"name"]; 
    if ([[item valueForKey:@"isRead"] boolValue] == NO) { 
     cell.imageView.image = [UIImage imageNamed:@"unread.png"]; 
    } else { 
     cell.imageView.image = nil; 
    } 
    cell.textLabel.font = [UIFont boldSystemFontOfSize:15.0]; 
    cell.textLabel.adjustsFontSizeToFitWidth = YES; 

    return cell; 
} 
+0

你是說你在進入詳細視圖之前某個特定的單元格會顯示一張照片,但在你回來之後不會顯示圖片嗎?它看起來像我正在緩存您的filteredResults值,並且如果您要更改幕後數據,我的猜測是過濾結果也不會被更新。 – 2012-07-08 03:27:59

+0

基本上,當我回到視圖的圖像應該更新,但它只適用於刪除搜索詞,並再次搜索 – Phillip 2012-07-08 07:53:31

+0

所以,正如我所說...它從代碼片段看起來像你緩存你的filteredResults。因爲它聽起來像是在改變幕後結果的內容,所以您需要再次執行搜索(如您所建議的那樣),或者當您在詳細視圖中更新某些內容時,您需要更新緩存的filteredResults以及其他結果。或者您需要更改filteredResults的內容,使其「項目不是您正在更改的內容的副本。 – 2012-07-08 17:27:31

回答

6

如果我理解你的權利,那麼你可以有一種變通方法,但具有相同的搜索字符串再次搜索:

if (self.searchDisplayController.active) { 
    self.searchDisplayController.searchBar.text = self.searchDisplayController.searchBar.text; 
} 

把它放在viewWillAppear:viewDidAppear:將被認爲顯示了每個時間(稱爲例如,您從detail view回到search view)。而在這個地方重新加載數據將是很好過,以獲得正確的數據(例如,如果您標記的細胞作爲示例代碼讀等)

只是[self.tableView reloadData];,而不是searchResultsTableView(它會自動使用新的搜索後更新的數據)

+0

令人驚歎的工作!現在圖像正在顯示(並沒有顯示)正確!最後,過去幾天,我的頭撞牆了! – Phillip 2012-07-08 23:25:56

0

聽起來好像也許你的細胞都被回收並不能正確復位。 UITableView使用視圖回收,所以重要的是如果你做了某些事情,比如設置圖像,那麼即使它們不是要顯示的圖像,也要確保它是明確設置的。

如果您分享您的cellsForRowAtIndexPath代碼,您可能可以獲得更多幫助。

+0

好的,用我的方法編輯! – Phillip 2012-07-07 08:33:51