I have cells that expand by changing their height with a setExpanded: method call.
I then call reloadRowsAtIndexPaths: to refresh the cells.
The problem is the c
@Javy,
Thanks for your question. I was developing table with similar behaviour: there are text views (UITextView
) in my table view cells (UITableViewCell
) that are :
So I found the same problem. In iOS 5.x when I was starting to type text it suddenly was becoming invisible. But in iOS 4.x everything works fine.
I have found the solution and it works well for me.
Solution: Just try to replace your animation type UITableViewRowAnimationAutomatic
with UITableViewRowAnimationNone
when reloading the particular cell.
Some additional code: reload both cell in one moment:
NSMutableArray *indexes = [NSMutableArray arrayWithCapacity:2];
// collapse previous cell
if( previousCell != nil && [previousCell expandable] )
{
if( [previousCell expanded] ) [previousCell setExpanded:NO];
[indexes addObject:previousIndexPath_];
}
// expand new cell
if( [cell expandable] )
{
[cell setExpanded:YES];
[indexes addObject:indexPath];
}
[tableView reloadRowsAtIndexPaths:indexes withRowAnimation:UITableViewRowAnimationNone];
Hope it will help you.
Seems to me like the cell isn't redrawn properly.
Did you try something like:
[cell.backgroundView setNeedsDisplay]
I had the same issue. I confirm that using no animation when reloading the row works fine.
But it turns out the issue is caused by returning a cached UITableViewCell in cellForRowAtIndexPath when actually a fresh one is needed.
The doc for reloadRowsAtIndexPaths:withRowAnimation: says: "Reloading a row causes the table view to ask its data source for a new cell for that row."
Your cellForRowAtIndexPath method is returning locally cached cells. Returning a brand new cell fixed the issue in my case and no workarounds were needed...
you could try reloading the table view when the animations have completed? at the moment if your animations are fired, and then you call [tableView reloadData]
the animations won't be complete, so the frame etc may get out of sync
you could change your set expanded method to take a flag as well as an animation completion block, which could contain the reload code
[cell setExpanded:YES completion:^
//reload code
}];
In:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
you must return a new cell. If cell you don't need to create a new one, you only need:
[tableView beginUpdates];
[tableView endUpdates];
There is no need to call reloadRowsAtIndexPaths
.
I don't see any difference in behavior between iOS 5.0 and 4.3.2 in the simulator, or 5.0 on my phone—the cells disappear in the same way on each. (I'd test on 4.2.1 on my older iPod touch but Xcode 4 can't. Grr..) It looks like there's an easy workaround, though: If you do both reloadRowsAtIndexPaths:withRowAnimation: after expanding/collapsing your cells and then the reloadData call after that, it appears to preserve the animation.
Here's a small demo project for this. It's hard to say what the actual problem was—it's just some odd side effect of how UIKit is doing the cell animation. If you subclass [UITableViewCell setAlpha:] you can see that the table view is setting the cell's alpha to 0 and leaving it there. Weird.
Edit: Well that's weird. It wasn't working for a bit (was doing the old behavior, with cells disappearing), but now it's right again. Maybe I was running the wrong version. Anyway, let me know if this works for you.