自定义 UITableViewCell 未在搜索上更新

发布于 2024-11-25 02:11:57 字数 1966 浏览 3 评论 0原文

我正在使用 uitableviewcontroller 和 uiSearchBar。在 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;
}

I'm using a uitableviewcontroller with a uiSearchBar. In the searchBarSearchButtonClicked method I get data and populate it in the uitableviewcontroller:

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

    NSDictionary *data = [self getData];

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

The problems is that I am using a custom uitableviewcell. When I click the search button new rows are returned, but the rows retrieved when the previous search was ran still remain.

For example if my initial search returned 1 result row and I do an additional search this time returning 3 rows, the first row will be from the first search and the subsequent two rows will be from the second search.

I reload the data in the view so I'm not sure why the cells are persisting. Here is my cellforrowatindexpath method where I create the cells:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

岁月染过的梦 2024-12-02 02:11:57

我认为问题出在数据源的使用上。我猜 tableData 是您用于向表格视图提供数据的数组,因为每次 - (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar 时都会刷新并加载新对象> 方法被调用。但是 tableData 没有在 cellForRowAtIndexPath 方法内的任何地方使用。从 tableData 数组向 tableView 提供数据可能会解决您的问题。

I think the problem is in using the data source. I guess tableData is the array you use for providing data to the tableview as it is flushed and loaded with new objects every time the - (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar method is called. But tableData is not used anywhere inside cellForRowAtIndexPath method. Providing data to tableView from tableData array may solve your problem.

_失温 2024-12-02 02:11:57

我想通了:

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

I figured it out:

 }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"];
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文