如何设置UITableViewCell高度的动画?

时间:2013-01-06 04:46:21

标签: iphone objective-c ios ipad

我有一个UITableView我想要它,这样当你点击单元格时,高度会动画到更高的高度(即:它会扩展)。我该怎么做?

1 个答案:

答案 0 :(得分:17)

实际上很简单,为NSIndexPath创建一个实例变量,我称之为selectedIndexPath。然后,您需要做的就是在heightForRowAtIndexPath中创建一个条件,以独立于其他单元调整此特定单元格的高度。

然后在didSelectRowAtIndexPath中将所选索引路径iVar设置为所选单元格的路径,并在表格上调用开始/结束更新。单元格将自动展开,并带有漂亮的动画。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    selectedIndexPath = indexPath;
    [self.tableView beginUpdates];
    [self.tableView endUpdates];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([indexPath compare:selectedIndexPath] == NSOrderedSame) {
        return 80;
    }
    return 40;
}