我在我的iPhone应用中使用UITableView,我有一个属于一个组的人的列表。我希望当用户单击一个特定的人(因此选择单元格)时,单元格的高度会增加,以显示用于编辑这个人的属性的几个UI控件。

这可能吗?


当前回答

我刚刚用一个小技巧解决了这个问题:

static int s_CellHeight = 30;
static int s_CellHeightEditing = 60;

- (void)onTimer {
    cellHeight++;
    [tableView reloadData];
    if (cellHeight < s_CellHeightEditing)
        heightAnimationTimer = [[NSTimer scheduledTimerWithTimeInterval:0.001 target:self selector:@selector(onTimer) userInfo:nil repeats:NO] retain];
}

- (CGFloat)tableView:(UITableView *)_tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
        if (isInEdit) {
            return cellHeight;
        }
        cellHeight = s_CellHeight;
        return s_CellHeight;
}

当我需要扩展单元格高度时,我设置isInEdit = YES并调用方法[self onTimer],它会动画单元格的增长,直到它达到s_cellhightediting值:-)

其他回答

我发现了一个非常简单的解决方案,作为一个UITableView的副作用,我正在.....工作

将单元格高度存储在一个变量中,该变量通常通过tableView: highightforrowatindexpath:来报告原始高度,然后当你想要动画化高度变化时,只需改变变量的值并调用这个…

[tableView beginUpdates];
[tableView endUpdates];

你会发现它不会完全重载,但足以让UITableView知道它必须重绘单元格,为单元格....抓取新的高度值你猜怎么着?它使你的改变变得生动。甜的。

我有一个更详细的解释和完整的代码示例在我的博客…动画UITableView单元格高度变化

我解决了reloadRowsAtIndexPaths。

我保存在didSelectRowAtIndexPath单元格选择的indexPath和调用reloadRowsAtIndexPaths在结束(你可以发送NSMutableArray的元素列表你想重新加载)。

在highightforrowatindexpath中,您可以检查indexPath是否在expandIndexPath单元格的列表中,并发送height。

你可以检查这个基本的例子: https://github.com/ferminhg/iOS-Examples/tree/master/iOS-UITableView-Cell-Height-Change/celdascambiadetam 这是一个简单的解决方案。

我加了一种代码来帮助你

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 20;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath: (NSIndexPath*)indexPath
{
    if ([indexPath isEqual:_expandIndexPath])
        return 80;

    return 40;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Celda";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    [cell.textLabel setText:@"wopwop"];

    return cell;
}

#pragma mark -
#pragma mark Tableview Delegate Methods

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSMutableArray *modifiedRows = [NSMutableArray array];
    // Deselect cell
    [tableView deselectRowAtIndexPath:indexPath animated:TRUE];
    _expandIndexPath = indexPath;
    [modifiedRows addObject:indexPath];

    // This will animate updating the row sizes
    [tableView reloadRowsAtIndexPaths:modifiedRows withRowAnimation:UITableViewRowAnimationAutomatic];
}

而不是beginUpdates()/ endpdates(),建议调用现在是:

tableView.performBatchUpdates(nil, completion: nil)

关于beginUpdates/ endpdates,苹果说:“尽可能使用performBatchUpdates(_:completion:)方法而不是这个方法。”

参见:https://developer.apple.com/documentation/uikit/uitableview/1614908-beginupdates

输入- - -

tableView.beginUpdates () tableView.endUpdates () 这些函数不会调用

func tableView(_ tableView: UITableView, cellForRowAt indexPath: indexPath) -> UITableViewCell {}

但是,如果你这样做了, 表视图。reloadRows (:(selectedIndexPath !, .none)

它会调用 func tableView(_ tableView: UITableView, cellForRowAt indexPath: indexPath) -> UITableViewCell {} 这个函数。

西蒙·李回答的简练版本:

tableView.beginUpdates()
tableView.endUpdates()

请记住,您应该在endpdates()之前修改height属性。