2012-05-04 45 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