如何实现UITableViewCell高度的动画效果?

4

我有一个UITableView,当你点击单元格时,我希望它的高度可以以动画形式增加(即扩展)。我该怎么做?

1个回答

17

其实很简单,创建一个实例变量来存储 NSIndexPath,我称其为 selectedIndexPath。然后在 heightForRowAtIndexPath 中做个判断,独立于其他单元格调整此特定单元格的高度即可。

接着在 didSelectRowAtIndexPath 中将选中的单元格的索引路径 iVar 设置为该索引路径,并在表格上调用 begin/end updates。这个单元格将自动以漂亮的动画扩展。

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接