2014-02-09 57 views
4

我想用UIView动画制作改变UITableViewCell高度的动画(以后用春天的动画)。改变UITableViewCell高度的自定义动画

所以我必须:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = (UITableViewCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) 
    { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
     cell.backgroundColor = [UIColor greenColor]; 
    } 
    UILabel *viewLabel = [[UILabel alloc] initWithFrame:CGRectMake(10.0f, 10.0f, [[UIScreen mainScreen] bounds].size.width, 40.0f)]; 
    viewLabel.text = @"Test"; 
    viewLabel.backgroundColor = [UIColor clearColor]; 
    [cell.contentView addSubview:viewLabel]; 

    return cell; 
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    [tableView beginUpdates]; 
    [self animateCell:indexPath andTableView:tableView]; 
    [tableView endUpdates]; 
} 


- (void)animateCell:(NSIndexPath*)indexPath andTableView:(UITableView*)tableView 
{ 
    [UIView animateWithDuration:1.0f animations:^
    { 
     UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
     CGRect rect = cell.frame; 
     rect.size.height = 90.0f; 
     cell.frame = rect; 
     NSLog(@"%f", cell.frame.size.height); 
    }]; 
} 

但它不工作。但是,如果我真的加入:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if ([indexPath isEqual:[tableView indexPathForSelectedRow]]) 
    { 
     return 90.0f; 
    } 
    return 40.0f; 
} 

我看到小区正在改变其高度和动画应用到在animateCell方法创建自定义单元格

+0

当你调用endUpdates,UITableView的执行默认的动画。动画属性基于表视图的数据源值。 – vokilam

+0

因此,我不需要调用beginUpdates和endUpdates并为自定义动画(将邻近单元格的位置更改为动画动画)进行处理? – Dimitrio

+1

你的动画应该是什么? – vokilam

回答

5

我认为你不应该使用beginUpdate/endUpdates方法和在致电reloadData之前手动执行动画。数据源定义了细胞的最终状态(动画之后)。这里是动画的tableView:didSelectRowAtIndexPath:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    _selectedIndexPath = indexPath; 

    [UIView animateWithDuration:1 animations:^{ 
     // move down cells which are below selected one 
     for (UITableViewCell *cell in tableView.visibleCells) { 
      NSIndexPath *cellIndexPath = [tableView indexPathForCell:cell]; 

      if (cellIndexPath.row > indexPath.row) { 
       CGRect frame = cell.frame; 
       frame.origin.y += kTargetHeight - tableView.rowHeight; 
       cell.frame = frame; 
      } 
     } 
     // expand selected cell 
     UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 

     CGRect frame = cell.frame; 
     frame.size.height = kTargetHeight; 
     cell.frame = frame; 
    } completion:^(BOOL finished) { 
     [tableView reloadData]; // commit final state 
    }]; 
} 

所选单元格的最终状态的例子:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if ([indexPath isEqual:_selectedIndexPath]) { 
     return kTargetHeight; 
    } 

    return tableView.rowHeight; 
} 
+0

感谢,它的工作,但有一个眨眼由于[tableView reloadData] – Dimitrio

+1

谢谢,它的工作,但有一个闪烁,因为[tableView reloadData]这给出更好的外观:[tableView beginUpdates]; [tableView endUpdates]; – Dimitrio

+0

你知道吗,didDselectRowAtIndexPath类似:并且不起作用 – Dimitrio

相关问题