【问题标题】:Custom UITableViewCell Not updating on Search自定义 UITableViewCell 未在搜索中更新
【发布时间】:2011-07-18 23:52:00
【问题描述】:

我正在使用带有 uiSearchBar 的 uitableviewcontroller。在 searchBarSearchButtonClicked 方法中,我获取数据并将其填充到 uitableviewcontroller 中:

 - (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {

    NSDictionary *data = [self getData];

    [self.tableData removeAllObjects];
    [self.tableData addObjectsFromArray:[data objectForKey:@"myKey"]];
    [self.view reloadData];
 }  

问题是我使用的是自定义的 uitableviewcell。当我单击搜索按钮时,会返回新行,但运行前一次搜索时检索到的行仍然存在。

例如,如果我的初始搜索返回了 1 个结果行,而我这次进行了额外的搜索返回 3 行,则第一行将来自第一次搜索,随后的两行将来自第二次搜索。

我在视图中重新加载数据,所以我不确定为什么单元格会持续存在。这是我创建单元格的 cellforrowatindexpath 方法:

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

UITableViewCell *cell = [tableView
                         dequeueReusableCellWithIdentifier:MyIdentifier];

NSDictionary *data = [self.tableData objectAtIndex:indexPath.row];

if (cell == nil) {
    cell = [[[UITableViewCell alloc] 
             initWithStyle:UITableViewCellStyleSubtitle 
             reuseIdentifier:MyIdentifier] autorelease];



myLabelDetail = [[UILabel alloc] initWithFrame:CGRectMake(152.0, 32.0, 60.0, 5.0)];
myLabelDetail.tag = INSTRUMENT_ID_LABEL_TAG;
myLabelDetail.font = [UIFont systemFontOfSize:14.0];
myLabelDetail.textAlignment = UITextAlignmentLeft;
myLabelDetail.textColor = [UIColor grayColor];
myLabelDetail.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleHeight;
myLabelDetail.text = [data objectForKey:@"title"];
[cell.contentView addSubview:myLabelDetail];
[myLabelDetail release];



cell.imageView.image = [UIImage imageNamed:@"icon"];
cell.textLabel.text = @"Title Text";
cell.detailTextLabel.text = @"My Label: ";


cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}else{
    myLabelDetail = (UILabel *)[cell.contentView viewWithTag:DETAIL_LABEL_TAG];
}

return cell;
}

【问题讨论】:

    标签: ios uitableview uisearchbar


    【解决方案1】:

    我认为问题在于使用数据源。我猜 tableData 是您用于向 tableview 提供数据的数组,因为每次 - (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar 方法被调用。但是 tableData 并没有在 cellForRowAtIndexPath 方法中的任何地方使用。从 tableData 数组向 tableView 提供数据可能会解决您的问题。

    【讨论】:

    • tabledata 位于 if 语句的正上方。这是你的意思吗?我将它移到 if 语句之外,但它仍然不起作用。
    • 您只是将 tableData 的特定对象分配给 data 使用此语句NSDictionary *data = [self.tableData objectAtIndex:indexPath.row]; 但在那之后,data 无处使用。
    • 我已经添加了我打算如何在单元格的 detailtext 标签中使用数据。这仍然有相同的结果。
    • 你能把你的- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section方法贴在这里吗?
    【解决方案2】:

    我想通了:

     }else{
          myLabelDetail = (UILabel *)[cell.contentView viewWithTag:DETAIL_LABEL_TAG];
          // i needed to add this to update when cell is already initialized
          myLabelDetail = [data objectForKey:@"title"];
     }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多