扩展单元代码无法正常工作

时间:2013-08-26 19:02:56

标签: objective-c uitableview

所以我一直在寻找一些代码,这些代码将在我的静态TableView中扩展和收缩我的单元格。我发现了一个论坛帖子,有人试图做同样的事情并借用了一些代码。

我基于TableViewController创建了一个自定义类。我添加了以下内容:

.h文件

NSIndexPath *selectedCellIndexPath;  

.m文件

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

[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if(selectedCellIndexPath != nil
   && [selectedCellIndexPath compare:indexPath] == NSOrderedSame)
    return 80;

return 40;
}

我将它用作我的TableView的viewController。

它有助于扩展和收缩我的表,但不是真的。这真的很笨拙。它没有正确的动画,似乎在弄乱我点击的行下方的行。

如何对此进行排序以制作动画并正常工作?

此外,加载视图时,如何将行高设置为指定的数字?

由于

1 个答案:

答案 0 :(得分:1)

您应该使用开始/结束更新,而不是重新加载选定的行。这将允许表自动进行必要的更改,并在它们之间设置动画。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView beginUpdates];

    if ([indexPath compare:selectedCellIndexPath] == NSOrderedSame) {
        selectedCellIndexPath = [NSIndexPath indexPathForRow:NSIntegerMax inSection:0];
    }else{
        selectedCellIndexPath = indexPath;
    }

    [tableView endUpdates];
}
相关问题