1

我有tableview一个自定义单元格和一个搜索栏。我已经在单元格上标记了标签,因为我imageView在那个单元格上也有一个标签。

该表显示良好的标签和图像,但在搜索中,方法 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;
}
4

2 回答 2

4

我发现了问题所在:searchDisplayController 返回了它自己的表格视图,因此我标记的单元格在此视图中不存在。为了使用自定义单元格,必须替换以下行:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

和:

UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
于 2012-05-05T12:18:54.880 回答
3

也许标签是单元格的子视图contentView。尝试

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

另外,请检查您searchDisplayController.searchResultsTableView的设置是否正确。听起来你没有恢复任何细胞。

于 2012-05-04T21:25:14.587 回答