2012-05-04 115 views
1

我有一個tableview與自定義單元格和搜索欄。我在單元上標記了標籤,因爲我在該單元上也有一個imageViewviewWith標籤不返回搜索對象

該表格顯示的標籤和圖像很好,但在搜索中,viewWithTag方法似乎無法找到標籤。只要我在搜索欄中輸入一個字符,視圖就會清除,就好像沒有找到標籤爲100的單元格一樣。 這裏是代碼:

- (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]; 
    } 

    if (tableView == self.searchDisplayController.searchResultsTableView) { 

     iKl_Stretch *stretchpb = [self.filteredStretchList objectAtIndex:indexPath.row];  
     UILabel *nameLabel = (UILabel *)[cell viewWithTag:100]; 
     nameLabel.text = stretchpb.name; 
     return cell; 

    } else { 

     iKl_Stretch *stretchpb = [self.categoryStretchList objectAtIndex:indexPath.row]; 
     UILabel *nameLabel = (UILabel *)[cell viewWithTag:100]; 
     nameLabel.text = stretchpb.name; 
    } 
    return cell; 
} 
+0

爲什麼你搜索與標籤100子視圖?你使用標準類UITableViewCell創建單元格,爲什麼你認爲在標準單元格內有一個標籤爲100的子視圖? – viggio24

+0

因爲我爲默認存在的那個標籤分配了一個值。這是不正確的? –

+0

你在哪裏設置每個單元的標籤標籤? –

回答

4

我發現什麼是錯的: searchDisplayController返回它自己的tableview,所以我的標記單元格不存在於這個視圖中。 爲了使用自定義的電池,必須更換行:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

有:

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

我花了兩天多的時間解決了這個問題!謝謝! – SevenDays

3

也許標籤是細胞的contentView的子視圖。嘗試

UILabel *nameLabel = (UILabel *)[cell.contentView viewWithTag:100]; 

另外,檢查您的searchDisplayController.searchResultsTableView是否設置正確。聽起來就像你沒有收回細胞。

+0

嘗試過,但它不起作用 –

+0

然後,您可能需要檢查搜索結果表的設置。 – Mundi