【问题标题】:UITableView cell expand animation changes when top margin is not fully visible当上边距不完全可见时,UITableView 单元格展开动画会发生变化
【发布时间】:2017-07-12 14:38:57
【问题描述】:

我尝试通过类似动画的下拉菜单使单元格展开/折叠,因此当表格视图重新计算高度时,它会向下移动其他单元格低于被点击的单元格,

但如果点击的单元格上边距不完全可见,表格视图会通过将所有上方单元格向上移动来改变其高度更新行为:

http://imgur.com/CldzuFf

有没有办法让单元格始终以自上而下的行为展开?
像这样重新加载单元格:

[self.tableView beginUpdates];
[self.tableView reloadRowsAtIndexPaths:arrayToReload withRowAnimation:UITableViewRowAnimationBottom];
[self.tableView endUpdates];

【问题讨论】:

  • 您的“显示更多/更少”功能使用什么方法?你在使用 UITableViewAutomaticDimension 的自动布局吗?或者您是否返回heightForRowAtIndexPath 中的特定值?还是您在插入/删除行?
  • 是的,UITableViewAutomaticDimension 具有开始/结束更新和行重新加载,如上所示。我还在estimatedHeightForRow 中返回预先计算的单元格高度。插入/删除行而不是重新加载似乎没有区别
  • OK - 取决于您更改显示/隐藏单元格内容约束的具体方式,无需调用reloadRowsAtIndexPaths...您可能会获得更好的结果...在单元格内部cellForRowAtIndexPath?中操作这些值
  • 所以我应该在更新括号内配置单元格高度约束而不是重新加载,这将需要精确的高度计算,然后我才返回来自 willDisplayCell 的高度值(实际上是上一次的高度显示单元格)。我会试试的。
  • 您应该能够使用约束而不是计算/分配特定值。有多种方法,有些实现可能以不同的方式更好地工作,但您可以在这里看到几种不同的方法:github.com/DonMag/DynamicCellHeight

标签: ios objective-c uitableview uikit


【解决方案1】:

只需根据所选行设置 contentOffset

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell * theCell = (UITableViewCell *)[tableView     
                                              cellForRowAtIndexPath:indexPath];

    CGPoint tableViewCenter = [tableView contentOffset];
    tableViewCenter.y += myTable.frame.size.height/2;

    [tableView setContentOffset:CGPointMake(0,theCell.center.y-65) animated:YES]; // need to customize the y offset 65 to your value
    // reload row here 
 }

【讨论】:

  • 我没有完全理解您的代码,但我尝试过处理动画表格视图偏移和 scrollToRowAtIndexPath 但它最终与其他动画(例如行重新加载或约束更新)发生冲突
猜你喜欢
  • 1970-01-01
  • 2018-07-28
  • 2019-06-14
  • 1970-01-01
  • 1970-01-01
  • 2017-11-11
  • 1970-01-01
  • 2012-03-08
  • 2012-07-26
相关资源
最近更新 更多