用于更改UITableViewCell高度的自定义动画

时间:2014-02-09 09:57:36

标签: ios uitableview uiview uiviewanimation

我想制作使用UIView动画更改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方法中创建的自定义单元格

1 个答案:

答案 0 :(得分: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;
}
相关问题