2012-05-04 5 views
1

私はtableviewにカスタムセルと検索バーがあります。私はそのセルにimageViewも持っているので、私はセルのラベルにタグを付けました。viewWithTagは検索でオブジェクトを返しません

テーブルはラベルと画像でうまく表示されますが、検索では、メソッドviewWithTagではタグ付きラベルが見つからないようです。検索バーに文字を入力すると、タグが100のセルが見つからなかったかのようにビューが消去されます。たぶんラベルは、セルのcontentViewのサブビューです

- (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

:で

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

を?標準クラスのUITableViewCellを使用してセルを作成していますが、なぜその標準セルの内部にタグ100のサブビューがあると思いますか? – viggio24

+0

私はそのタグにデフォルトで存在する値を割り当てたので。これは間違っていますか? –

+0

各セルのラベルタグを設定する場所はどこですか? –

答えて

4

私は何が間違っているかを見つけました: searchDisplayControllerは独自のテーブルビューを返します。タグ付きセルはこのビューに存在しません。 カスタムセルを使用するためには、ライン交換する必要があります:あなたがタグ100とのサブビューを検索なぜ

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

私はこの問題を2日以上解決するのに費やしました!ありがとうございます! – SevenDays

3

: はここのコードです。また

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

を試してみてください、あなたのsearchDisplayController.searchResultsTableViewが正しく設定されているかを確認。あなたが細胞を取り戻しているように聞こえる。

+0

が試行されましたが動作しません –

+0

次に、検索結果テーブルの設定を確認する必要があります。 – Mundi

関連する問題