UITableViewCell扩展动画?

时间:2014-08-02 14:09:29

标签: ios uitableview

我想制作像可扩展细胞一样的东西。我想要实现的结果是: What I want

其实我有:

What I have

这是我的代码:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    DoctorCell *cell = self.prototypeCell;
    DoctorModel *doctor = [self.fetchController objectAtIndexPath:frcIndexPath];

    // depending on isActive property I add (or do not add) some content.
    [cell cellForDoctor:doctor isActiveCell:[self.activeCellIndexPath isEqual:indexPath]];
    [cell setNeedsUpdateConstraints];
    [cell setNeedsLayout];
    [cell layoutIfNeeded];

    CGFloat height = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
    return height + 1;
}

这是我的细胞选择方法

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    NSIndexPath *oldIndexPath = self.activeCellIndexPath;
    if ([indexPath isEqual:self.activeCellIndexPath]) {
        self.activeCellIndexPath = nil;
        oldIndexPath = nil;
    } else {
        self.activeCellIndexPath = indexPath;
    }

    [self.doctorTableView beginUpdates];
    [self.doctorTableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, oldIndexPath, nil] withRowAnimation:UITableViewRowAnimationNone];
    [self.doctorTableView endUpdates];

    [self.doctorTableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
}

正如你所看到的,我的动画看起来很奇怪(有点移动到错误的位置,跳跃等)。我做错了什么?或者我是否需要选择另一种扩展细胞的方法?

1 个答案:

答案 0 :(得分:3)

您可以像这样使用UITableView动画自动化:

  1. 致电[tableView beginUpdates];
  2. 更改数据源。
  3. 插入或删除行和部分,或重新加载它们
    • 这应该会导致计算新的高度等。
    • 确保使用适合这些方法的withRowAnimation参数的内容
  4. 致电[tableView endUpdates];
  5. 您的动画现在应该按预期执行。

    也许您的问题是您为行动画选择了UITableViewRowAnimationNone

相关问题