5

在选择一条推文时,我希望具有与 Twitter 应用程序相同的行为:扩展行并添加补充内容。

所以这不仅仅是一个基本的行大小调整。我想我需要 2 个不同的自定义单元格,所以我做了类似的事情:

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([indexPath compare:self.selectedIndexPath] != NSOrderedSame) {
        FirstCell *cell = [tableView dequeueReusableCellWithIdentifier:@"FirstCell"];
        if (!cell) {
            cell = [[FirstCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"FirstCell"];
        }

        cell.firstnameLabel.text = [[self.items objectAtIndex:indexPath.row] objectForKey:@"firstname"];

        return cell;
    }
    else {
        SecondCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SecondCell"];
        if (!cell) {
            cell = [[SecondCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"SecondCell"];
        }

        cell.firstnameLabel.text = [[self.items objectAtIndex:indexPath.row] objectForKey:@"firstname"];
        cell.lastnameLabel.text = [[self.items objectAtIndex:indexPath.row] objectForKey:@"lastname"];

        return cell;
    }

}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([indexPath compare:self.selectedIndexPath] == NSOrderedSame) {
        return 80.0;
    } else {
        return 44.0;
    }
}

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.selectedIndexPath = indexPath;

    [tableView reloadData];
}

我的问题是我想保持像 Twitter 应用程序这样的流畅动画,这不是我的情况,因为[tableView reloadData](我认为这是强制性的,因为我有 2 个不同的自定义单元格)。

所以有人知道我需要的解决方法,或者有人知道 Twitter 应用程序是如何处理这个动画的吗?

4

3 回答 3

7

您想用动画重新加载该单元格:

[tableView beginUpdates];
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[tableView endUpdates];
于 2013-02-19T21:30:30.353 回答
4
[tableView beginUpdates];
[tableView endUpdates];

这会触发整个 tableView 的单元格行大小的更新......引用自:iPhone - UITableViewCell 高度变化的平滑动画,包括内容更新

快乐编码!

于 2013-02-19T21:26:14.503 回答
1

实际上,只需将原始代码与 rooster117 的解决方案相结合,就可以使用两个自定义单元来实现这一点,就像您最初想要的那样。替换[tableView reloadData]tableView:didSelectRowAtIndexPath:

[tableView beginUpdates];
[tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
[tableView endUpdates];

你应该有你的解决方案。

于 2013-07-29T03:58:13.417 回答