Animating UITableView update, succeeded but needs more detail
I want to show animation when add cells to UITableView.
Here's is what I implemented (pseudo code)
[self.tableView beginUpdates];
// remove row exists
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:(rows exists) withRowAnimation:UITableViewRowAnimationFade];
(chang data source here, for me, it's NSFetchedResultsController)
// insert new rows
[self.tableView insertRowsAtIndexPaths:(new rows) withRowAnimation:UITableViewRowAnimationFade];
[self.tableView endUpdates];
This code shows animation well, but it has a small开发者_运维百科 problem.
The cells are shows moving animation from frame rect (0, 0, 0, 0) to it's actual position, not only fading animation.
I think problem is that initial frame of cells are (0, 0, 0, 0), so I set up initial frame property of cell in cellForRowAtIndexPath, but it does not work.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
....
cell.frame = CGRectMake(0, indexPath.row * 64, 320, 64);
NSLog(@"set frame");
....
}
How can I show shade anmation only, with no cell moving animation?
The code is untested, but the idea should work:
BOOL animateRowsAlpha = NO;
- (void)reloadData {
[UIView animateWithDuration:0.2
animations:^{
for (UITableViewCell *cell in self.tableView.visibleCells) {
cell.alpha = 0.0f;
}
} completion:^(BOOL finished) {
animateRowsAlpha = YES;
[self.tableView reloadData];
}
];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = @"Cell";
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell)
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];
if(animateRowsAlpha)
cell.alpha = 0.0;
return cell;
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
if (!animateRowsAlpha) {
return;
}
[UIView animateWithDuration:0.2
animations:^{
cell.alpha = 1.0f;
}];
NSArray *indexPaths = [tableView indexPathsForVisibleRows];
NSIndexPath *lastIndexPath = [indexPaths lastObject];
if(!lastIndexPath || [lastIndexPath compare:indexPath] == NSOrderedSame) {
animateRowsAlpha = NO;
}
}
精彩评论