Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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 cell are 3 UIViews containing a label.

The goal is to expand the cell when tapped, then collapse it when tapped again. The cell itself must change it’s height to expose the subviews. Inserting or removing rows is unacceptable.

The demo project works almost as expected. In fact, in iOS 4.3 it works perfect. Under iOS 5, however, when the rows collapse, the previous cells magically disappear.

To re-create the problem, run the project in the simulator or device with iOS 5 and tap the first cell to expand it. Then tap the cell again to collapse it. Finally, tap the cell directly underneath it. The previous one disappears.

Continuing the tapping for each cell in the section will cause all the cells to disappear, to where the entire section is missing.

I’ve also tried using reloadData instead of the current setup, but that ruins the animations and feels a bit like a hack anyway. reloadRowsAtIndexPaths should work, but the question is why doesn’t it?

See images of what's happening below:

Table appears:

table appears

Cell expands:

table expands

Cell collapses:

table collapses

Cell disappears (when tapping the cell underneath):

cell disappears

Keep repeating until the entire section disappears:

section disappears

EDIT: Overriding the alpha is a hack, but works. Here is another 'hack' that fixes it as well but WHY does it fix it?

JVViewController.m line 125:

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];         [indicesToReload addObject:[previousIndexPath_ copy]];     }     else if( currentCellSameAsPreviousCell )     {         [previousCell setExpanded:YES];         [indicesToReload addObject:[previousIndexPath_ copy]];     }      //[indicesToReload addObject:[previousIndexPath_ copy]]; } 

EDIT 2:

Made a few minor changes to demo project, worth checking out and reviewing JVViewController didSelectRowAtIndexPath method.