Why do my table view cells disappear when reloaded using reloadRowsAtIndexPaths?

后端 未结 12 1780
青春惊慌失措
青春惊慌失措 2021-02-02 12:20

I have a bare-bones sample project here:

http://dl.dropbox.com/u/7834263/ExpandingCells.zip

In this project, a UITableView has a custom UITableViewCell. In each

12条回答
  •  旧巷少年郎
    2021-02-02 12:38

    I think you should not retain the previous indexPath when the cell is not expanded, try by modifying you did select method like the below, its working fine..

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
    BOOL currentCellSameAsPreviousCell = NO;
    NSMutableArray *indicesToReload = [NSMutableArray array];
    
    if(previousIndexPath_ != nil)
    {
        if( [previousIndexPath_ compare:indexPath] == NSOrderedSame ) currentCellSameAsPreviousCell = YES;
    
        JVCell *previousCell = (JVCell*)[self cellForIndexPath:previousIndexPath_];
    
        BOOL expanded = [previousCell expanded];
        if(expanded) 
        {
         [previousCell setExpanded:NO];
        }
        else if  (currentCellSameAsPreviousCell)
        {
            [previousCell setExpanded:YES];
        }
        [indicesToReload addObject:[previousIndexPath_ copy]];
    
        if (expanded)
            previousIndexPath_ = nil;
        else
            previousIndexPath_ = [indexPath copy];         
    }
    
    if(currentCellSameAsPreviousCell == NO)
    {
        JVCell *currentCell = (JVCell*)[self cellForIndexPath:indexPath];
    
        BOOL expanded = [currentCell expanded];
        if(expanded) 
        {
            [currentCell setExpanded:NO];
            previousIndexPath_ = nil;
        }
    
        else
        {
            [currentCell setExpanded:YES];
            previousIndexPath_ = [indexPath copy];
        }
    
        // moving this line to inside the if statement blocks above instead of outside the loop works, but why?
        [indicesToReload addObject:[indexPath copy]];
    
    
    }
    
    // commenting out this line makes the animations work, but the table view background is visible between the cells
    
    [tableView reloadRowsAtIndexPaths:indicesToReload withRowAnimation:UITableViewRowAnimationAutomatic];
    
    // using reloadData completely ruins the animations
    [tableView beginUpdates];
    
     [tableView endUpdates];
    }
    

提交回复
热议问题