自定义 UITableViewCell 未在搜索上更新
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为问题出在数据源的使用上。我猜 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.
我想通了:
I figured it out: