【发布时间】:2013-11-28 06:40:54
【问题描述】:
我在动画添加或删除 UITableView 中的行时遇到问题,该行的高度与其他行不同。
以下 GIF 演示了默认高度 (44pts) 的行和插入和删除更大行 (100pts) 的问题。左边是模拟器的屏幕录像(新单元格最终覆盖第五行是一个不同的问题),右边是它应该做什么的模型。
就我而言,我有一堆行,每行 60pts 高。当点击单元格中的按钮时,“编辑”单元格将从下方滑出,将下方的单元格向下推。此编辑单元格高 180 点。当我调用 insertRowsAtIndexPaths:withRowAnimation: 或 deleteRowsAtIndexPaths:withRowAnimation: 时,动画假定错误的高度为 60pts,而不是 180pts,它应该是
这意味着在UITableViewRowAnimationTop 的情况下,新单元格出现在距离它最终位置的-60pts 处,并向下滑动到新位置;大约三分之一的动画应该做。同时,下面的行从其起始位置平滑地动画到向下 180 点,完全符合其应有的效果。
有没有人为此制定了实际的解决方案?有什么方法可以告诉新行动画应该是什么高度?
下面是我用来隐藏和显示编辑行的代码。我使用TLSwipeForOptionsCell 来触发编辑,但它很容易复制使用例如tableView:didSelectRowAtIndexPath:
-(void)hideEditFields{
[self.tableView beginUpdates];
[self.tableView deleteRowsAtIndexPaths:@[[NSIndexPath indexPathForItem:editFormVisibleForRow+1 inSection:0]] withRowAnimation:UITableViewRowAnimationTop];
editFormVisibleForRow = -1;
[self.tableView endUpdates];
}
-(void)cellDidSelectMore:(TLSwipeForOptionsCell *)cell{
NSIndexPath* indexPath = [self.tableView indexPathForCell:cell];
// do nothing if this is the currently selected row
if(editFormVisibleForRow != indexPath.row){
if(editFormVisibleForRow >= 0){
[self hideEditFields];
// update the index path, as the cell positions (may) have changed
indexPath = [self.tableView indexPathForCell:cell];
}
[self.tableView beginUpdates];
editFormVisibleForRow = indexPath.row;
[self.tableView insertRowsAtIndexPaths:@[
[NSIndexPath indexPathForItem:editFormVisibleForRow+1 inSection:0]
] withRowAnimation:UITableViewRowAnimationTop];
[self.tableView endUpdates];
}
}
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return _dataSource.count + (editFormVisibleForRow >= 0 ? 1 : 0);
}
-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
int row = indexPath.row;
if(editFormVisibleForRow >= 0 && row > editFormVisibleForRow && row <= editFormVisibleForRow + 1){
return 180.0f;
}
else return 60.0;
}
四处寻找,似乎这是一个没有明确答案的常见问题。我在这里找到的关于 SO 的大多数类似问题都没有得到解答,或者提供了针对提问者情况的解决方法。 (例如:Problem with RowAnimation、Custom UITableViewCell height yields improper animation、UITableView animation glitch when deleting and inserting cells with varying heights)。
另外,我没有尝试制作一个三倍大小的编辑行,而是尝试制作三个较小的行并为它们设置动画,但这并不合适,因为它们都同时出现。我还尝试一个接一个地为它们设置动画,但缓动让它看起来很奇怪,出现了一个明显的 3 步动画,而不是整个编辑视图在一个动作中滑出视图。
编辑:我刚刚注意到,如果我为我要制作动画的那一行上方的行调用reloadRowsAtIndexPaths:withRowAnimation: UITableViewRowAnimationNone,它会改变动画的行为;即动画假设高度为0pts,如下动画所示。它更接近我想要的,但仍然不正确,因为动画速度是错误的并且留下了差距(在我的应用程序中,这意味着背景
颜色戳穿)
【问题讨论】:
-
你想明白了吗?尝试在这里做类似的事情......谢谢
-
不幸的是,客户改变了我们的设计,不再需要这个功能,所以我从来没有解决过。如果我再次遇到它并解决它,我一定会回答这个问题
-
遇到同样的问题
-
我遇到了同样的问题。有人解决了这个问题吗?
-
所以没有解决方法?从现在开始 2 年了?
标签: ios animation uitableview