2015-05-04 79 views
0

我有UITableViewCell指向顶部或底部的详细箭头。在重新加载数据时在TableViewCell中的动画

比我点击这个细胞我想扩大它。所以我会重新加载UITableView and I want to animate rotation of UIImageView`。

问题 - 是否可以动画旋转UIImageView同时重新加载数据为UITableView

+0

是的,可以同时为图像视图设置动画和扩展单元格。要为展开的单元格设置动画效果,请使用' - (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)'UITableView'的动画方法。对于图像动画,使用“UITableViewCell”的自定义子类并处理该类中的水龙头和动画。 – Akhilrajtr

+0

Akhilrajtr,谢谢,以及如何保存单元格状态的最佳方式(简单/详细)?在VC中的数组? –

+1

如果您有一个对象的数据源数组(从中填充tableview),请将该单元格的状态保存在该对象本身中。这将是最好的解决方案。如果您没有数据源对象数组,请保留一个'NSMutableArray'来保存/添加扩展单元格的NSIndexPath。 – Akhilrajtr

回答

0

是的,您可以创建一个带有可展开/可联系单元格的表格,也可以在展开时为单元格的容器设置动画。

但是,除非需要重新加载表格,否则我会建议您使用同时执行的动画重新加载行。

此外,当您在评论中询问什么是跟踪单元格是否需要显示简单或详细的最佳方式时,我认为这取决于您的要求,例如,如果您需要一次只显示一个展开的单元格,如果您需要一次显示多个展开的单元格,则只需将当前展开的单元格的索引保留在该类中,而不是使用数据源来保存该单元格是否需要显示为简单或详细的信息。如果你没有数据源,那么你可以通过创建一个单元格的索引路径列表来跟踪详细的单元格。

编程,你需要做以下的事情表现出简单/详细的细胞 -

注意:我假设只显示一次一个扩展单元。

1)创建一个成员变量来跟踪展开的单元格。

NSIndexPath *expandedCellIndexPath; 

2),用于扩增的细胞(如果膨胀返回高度)否则为简单细胞

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if([expandedCellIndexPath compare:indexPath] == NSOrderedSame) 
     return 100.0f; 

    return 50.0f; 
} 

3)原样

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *cellIdentifier = @"cellId"; 

    CustomCell *customCell = nil; 

    customCell = (CustomCell *) [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 

    if(customCell == nil) 
    { 
     NSString *nibName = @"CustomCell_iPhone"; 

     customCell = (CustomCell *)[[[NSBundle mainBundle] loadNibNamed:nibName owner:self options:nil] lastObject]; 

     customCell.selectionStyle = UITableViewCellSelectionStyleNone; 
    } 

    if([expandedCellIndexPath compare:indexPath] == NSOrderedSame) 
    { 
     [customCell.m_arrowImageView setImage:[UIImage imageNamed:DOWN_ARROW_KEY]]; 
     [self showSelectedCellAnimations:customCell]; 
    } 
    else 
    { 
     [customCell.m_arrowImageView setImage:[UIImage imageNamed:UP_ARROW_KEY]]; 
     [self showUnSelectedCellAnimations:customCell]; 
    } 

    // you can also set default up arrow image and rotate it with down arrow image and vice versa 

    return customCell; 
} 

4)旋转上/下箭头图像创建单元using-

- (void)rotateImage:(UIImageView *)image duration:(NSTimeInterval)duration 
       curve:(int)curve degrees:(CGFloat)degrees 
{ 
    [UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationDuration:duration]; 
    [UIView setAnimationCurve:curve]; 
    [UIView setAnimationBeginsFromCurrentState:YES]; 

    CGAffineTransform transform = 
    CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(degrees)); 
    image.transform = transform; 

    [UIView commitAnimations]; 
} 

5)呼叫上述方法原样

[self rotateImage:customCell.m_helpImage duration:0.25 
          curve:UIViewAnimationCurveEaseIn degrees:0.0]; 

6)内部didSelectRowAtIndexPath:设置expandedCellIndexPath到需要扩大并重新加载前扩大行收缩和重新加载当前expandedCellIndexPath行扩大当前索引路径,喜欢 -

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    [tableView deselectRowAtIndexPath:indexPath animated:NO]; 

    CustomCell *customCell = nil; 

    customCell = (CustomCell *)[tableView cellForRowAtIndexPath:indexPath]; 

    NSIndexPath *previousIndexPath = expandedCellIndexPath; 

    if([expandedCellIndexPath compare:indexPath] != NSOrderedSame) 
      expandedCellIndexPath = indexPath; 

    [self.m_tableView reloadRowsAtIndexPaths:@[previousIndexPath] 
          withRowAnimation:UITableViewRowAnimationFade]; 

    [self.m_tableView reloadRowsAtIndexPaths:@[expandedCellIndexPath] 
          withRowAnimation:UITableViewRowAnimationFade]; 

    [self.m_tableView scrollToRowAtIndexPath:indexPath 
          atScrollPosition:UITableViewScrollPositionBottom animated:YES]; 
} 
相关问题