Ios 清除数据源,重新加载数据,单元格仍有旧信息? 擦除数据源并重新加载数据后,tableviewcells仍显示旧信息。
我正在创建一个tableview,它显示数据库中用户的搜索结果 执行搜索时,将填充数据源并添加用户:Ios 清除数据源,重新加载数据,单元格仍有旧信息? 擦除数据源并重新加载数据后,tableviewcells仍显示旧信息。,ios,objective-c,uitableview,ios7,Ios,Objective C,Uitableview,Ios7,我正在创建一个tableview,它显示数据库中用户的搜索结果 执行搜索时,将填充数据源并添加用户: - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 对于创建的每个单元格,将创建一个关联的配置文件照片和按钮,并将其添加到当前单元格中 我有一个只用于测试的方法,它清除数据源并重新加载所有单元格 - (IBAction)cleardaTabl
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
对于创建的每个单元格,将创建一个关联的配置文件照片和按钮,并将其添加到当前单元格中
我有一个只用于测试的方法,它清除数据源并重新加载所有单元格
- (IBAction)cleardaTables:(id)sender {
[_dataSource removeAllObjects];
[self.tableView reloadData];
}
如果我用该按钮清除搜索,然后搜索其他内容,或者显示一个显示“搜索用户”的占位符单元格,我的占位符单元格仍会在其位置显示前一个单元格中的按钮
有人知道如何真正清除细胞数据和显示新鲜细胞吗
代码-抱歉弄乱了
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *searchResultCell = @"searchResultCell";
EIConnectionsGlobalSearchResultCell *cell = [tableView dequeueReusableCellWithIdentifier:searchResultCell];
if (cell == nil) {
cell = [[EIConnectionsGlobalSearchResultCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:searchResultCell];
}
//Create "no results" cell if datasource is empty after search
if (_emptySearch == 1)
{
cell.imageView.image = nil;
cell.labelName.text = @"No results...";
cell.labelConnectionStatus.text = @"Try another search.";
cell.labelConnectionStatus.textColor = [UIColor grayColor];
[cell setUserInteractionEnabled:NO];
return cell;
}
else if (_emptySearch == 0)
{
//Assigning Search Result
NSMutableDictionary *searchResult = [_dataSource objectAtIndex:indexPath.row];
//Text Configs
cell.labelName.text = [searchResult objectForKey:@"Name"];
//Assign and Convert ConnectionStatus Number to string and color
int connectionStatusInt = [[searchResult objectForKey:@"ConnectionStatus"] intValue];
NSMutableArray *connectionStatus = [self connectionStatusToString:connectionStatusInt withIndexPath:indexPath];
cell.labelConnectionStatus.text = connectionStatus[0];
cell.labelConnectionStatus.textColor = connectionStatus[1];
//Setting up profile photo for cell
if ([[searchResult objectForKey:@"picDownloaded"] isEqualToString:@"no"] || ![searchResult objectForKey:@"picDownloaded"]) {
if (self.tableView.dragging == NO && self.tableView.decelerating == NO)
{
[self startProfilePicDownload:searchResult andIndex:indexPath];
}
//Setting Default Image while downloading
cell.imageView.image = defaultProfileImage;
}
else
{
//A profile pic exists in cache, so assign it to the view
cell.imageView.image = [searchResult objectForKey:@"profilePic"];
}
//setting up cell actions
UIButton *cellAction = [UIButton buttonWithType:UIButtonTypeCustom];
cellAction.frame = CGRectMake(cell.frame.size.width - 52, 18.0f, 25.0f, 25.0f);
UIImage *buttonImage = [UIImage imageNamed:@"Arrow-Icon"];
[cellAction setImage:buttonImage forState:UIControlStateNormal];
cellAction.imageView.contentMode = UIViewContentModeScaleAspectFit;
[cellAction setTag:indexPath.row];
[cellAction addTarget:self action:@selector(showCellActionOptions:) forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:cellAction];
return cell;
}
return cell;
}
细胞被重新利用。在滚动表视图时以及在重新加载时都会发生这种情况 您遇到的一个大问题是,当
\u emptySearch
等于0时,每次重复使用一个单元格时,都会向该单元格添加另一个ui按钮。但同样的电池也可以在其他条件下重复使用
你有几个选择
创建两个不同的自定义单元格。一个用于当\u emptySearch
等于0时所需的行,另一个用于当它等于1时所需的行。在这种情况下,将按钮添加到IB中,而不是将其添加到cellforrowatinexpath
中
保留您拥有的代码,但您需要确保只添加一次按钮,并且删除不需要按钮的单元格
还可以查看UITableViewCell prepareforeuse
方法。您应该实现此方法(并调用super prepareforeuse
)根据需要重置单元格标签和图像。显示cellforrowatinexpath
方法。很可能您正在添加从未删除过的子视图。@rmaddy谢谢您的查看-我已经添加了我的代码。我认为您是对的,尽管@rmaddy-我没有明确地从该单元格中删除按钮或配置文件图像。我只是认为重新加载一个清晰的数据源会清除单元格。在重新使用手机之前,我该如何移除这些东西?谢谢你的帮助-我会看看是否能按照你的建议修复它