【发布时间】: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