专注-谆享

----------------木秀于林

animation tableview

[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"); 
    .... 
} 


 

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; 
    } 
} 


 

Will above code work for deleting/inserting node into tableview
阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/gnicky/article/details/7395477
个人分类: iOS
上一篇iOS杂记
下一篇关于iOS项目开发控制
想对作者说点什么? 我来说一句

ios 可折叠的table

2012年12月27日 31KB 下载

没有更多推荐了,返回首页

关闭
关闭