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

问题描述:

我有一个UITableView,它具有2个不同的自定义UITableViewCell以及2个不同的唯一标识符。上一行的UITableViewCell内容在单元重新加载时仍然可见

我加载Load上的第一个自定义UITableViewCell,然后加载Cell Select上的第二个自定义UITableViewCell

我知道我的问题与我重复使用我的单元格的方式有关。但我试过使用

routineSearchSelectedResultCell * cell = (routineSearchSelectedResultCell*)[tableView dequeueReusableCellWithIdentifier:nil];并且结果是新的UITableViewCell是空的,并且属性没有被填充。

我也试过[[[cell contentView] subviews] 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 [email protected]"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]; 
    } 



} 

+0

这样的IM假设你指定'IDP。行“的单元格选择,然后调用'reloadData'? – KDaker

+0

我刚刚添加了'didSelectRowAtIndexPath'的代码 –

+0

那么究竟是什么问题呢?从图片看来,这个单元格正在加载。如果你重新加载,你是否期待它消失? – KDaker

尝试重写

-(void)prepareForReuse 

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

看到document

我认为你正在使用的标签,它们是重叠.. 尝试使用此,它可以帮助你..

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