Ios 重新加载单元格时,上一行的UITableViewCell内容仍然可见

Ios 重新加载单元格时,上一行的UITableViewCell内容仍然可见,ios,objective-c,cocoa-touch,uitableview,Ios,Objective C,Cocoa Touch,Uitableview,我有一个UITableView,它有两个不同的自定义UITableView单元格,具有两个不同的唯一标识符 我在load上加载第一个自定义UITableViewCell,然后在单元格选择上加载第二个自定义UITableViewCell 我知道我的问题与我重复使用手机的方式有关。但我试过使用 routineSearchSelectedResultCell*单元格=(routineSearchSelectedResultCell*)[tableView dequeueReusableCellWith

我有一个
UITableView
,它有两个不同的自定义
UITableView单元格
,具有两个不同的唯一标识符

我在
load
上加载第一个自定义
UITableViewCell
,然后在
单元格选择上加载第二个自定义
UITableViewCell

我知道我的问题与我重复使用手机的方式有关。但我试过使用

routineSearchSelectedResultCell*单元格=(routineSearchSelectedResultCell*)[tableView dequeueReusableCellWithIdentifier:nil]
结果是新的
UITableViewCell
为空,并且不会填充属性

我还尝试了
[[cell contentView]子视图]makeObjectsPerformSelector:@selector(removeFromSuperview)]但给我相同的结果

我怎样才能绕过这个问题

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    if (idp && idp.row == indexPath.row  ) {
        return [self tableViewRoutineSelectedResult:tableView cellForRowAtIndexPath:indexPath];
    }

    NSString *cellIdentifier = @"routineSearchCell";
    routineSearchCell * cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    if (!cell) {
        cell = [[routineSearchCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
    }


    cell.routineName.text = _myDownloadedInfo[@"routines"][indexPath.row][@"routine"][@"routineName"];
    cell.routineAuthor.text = _myDownloadedInfo[@"routines"][indexPath.row][@"routine"][@"username"];

    return cell;

}
- (routineSearchSelectedResultCell *)tableViewRoutineSelectedResult:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    NSDictionary* myRoutine = _myDownloadedInfo[@"routines"][indexPath.row][@"routine"];

    [self sortOutRoutineImages:myRoutine[@"routineType"]];

    NSString *cellIdentifier = @"routineSearchSelectedResultCell";
    routineSearchSelectedResultCell * cell = (routineSearchSelectedResultCell*)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (!cell) {
        cell = [[routineSearchSelectedResultCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
    }

    cell.label1.text =@"test";
    cell.contentView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"BlackGradientSearch"]];
    cell = [self iconRoutineImagesController:cell];
    cell.imgInstruction.image = [UIImage imageNamed:@"InstructionSearchWhite"];
    cell.imgVideos.image = [UIImage imageNamed:@"VideoSearchWhite"];
    cell.routineName.text = myRoutine[@"routineName"];
    [cell.download setBackgroundImage:[UIImage imageNamed:@"DownloadSearchWhite"] forState:UIControlStateNormal];
    return cell;
}
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    if ( idp ) {
        NSIndexPath* pIdp = [[NSIndexPath alloc] init];
        pIdp = idp;
        idp = indexPath;
        [tableView beginUpdates];
        [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
        [tableView reloadRowsAtIndexPaths:@[pIdp] withRowAnimation:UITableViewRowAnimationRight];
        [tableView endUpdates];
    } else {
        idp = [[NSIndexPath alloc]init];
        idp = indexPath;
        [tableView beginUpdates];
        [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
        [tableView endUpdates];
    }



}
尝试覆盖

-(void)prepareForReuse
如果UITableViewCell对象是可重用的,也就是说,它具有重用标识符。在从UITableView方法dequeueReusableCellWithIdentifier返回对象之前,将调用此方法。出于性能原因,您应该只重置与内容无关的单元格属性,例如alpha、编辑和选择状态。重用单元格时,tableView:cellForRowAtIndexPath:中表视图的委托应始终重置所有内容。如果单元对象没有关联的重用标识符,则不会调用此方法。如果重写此方法,则必须确保调用超类实现


请看

我想您使用的是标签,它们重叠了。。 试着用这个,它可能会对你有帮助

for(UIView *view in cell.subviews){ 
   if([view isKindOfClass:[UILabel class]]){
     [view removeFromSuperview];
   } 
}

因此,我假设您在单元格选择中分配
idp.row
,然后调用
reloadData
?我刚刚添加了
didselectrowatinexpath
的代码,那么到底是什么问题?从图片上看,电池似乎正在加载。如果你重新加载,你希望它消失吗?问题是它加载了,但它加载在以前的内容之上(参见图)。我不想显示以前的内容。我只是好奇,如果你要替换
beginUpdates]。。。endUpdates]
使用
[tableView reloadData]
,它会工作吗?你能试试吗