ios – 可变高度UITableViewCell在滚动到列表结尾并旋转后展开

前端之家收集整理的这篇文章主要介绍了ios – 可变高度UITableViewCell在滚动到列表结尾并旋转后展开前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
UPDATE

我知道造成这种奇怪的布局的原因是什么.它是附件(UITableViewCellAccessory)的设置.如果我停止指定附件,布局不会中断.我没有添加这个作为答案,因为答案需要一个解决方案,给我一个配件,而不打破布局

我看到人们使用动态高度自定义单元的大多数问题是它们在旋转之前没有正确的高度.但是我看到了相反的情况:所有单元格的高度都适用于其动态内容.向上和向下滚动不会破坏这一点.但是,如果我滚动到列表的底部,然后旋转设备,然后向后旋转一行将成为屏幕高度的0.5到1.5倍.

进一步旋转或进一步滚动将使行返回到期望高度.我在屏幕截图之前和之后都包含了几个

UITableView的定义如下

  1. this.rootChildrenTable = new UITableView()
  2. {
  3. TranslatesAutoresizingMaskIntoConstraints = false,AccessibilityIdentifier = "rootChildrenTable",RowHeight = UITableView.AutomaticDimension,EstimatedRowHeight = 44.0f,BackgroundColor = UIColor.GroupTableViewBackgroundColor,TableFooterView = new UIView(),TableHeaderView = this.searchBar,KeyboardDismissMode = UIScrollViewKeyboardDismissMode.OnDrag
  4. };

注意通常的嫌疑人是RowHeight和EstimatedRowHeight.一旦我从标签删除Lines = 0,使行的高度相同,问题就会消失.

知道还有什么我应该看的吗?

解决方法

我在项目中遇到了同样的问题.我通过以下方式克服了这一点,为行设置了高度.
  1. //calculate the height by this way
  2. override func tableView(_ tableView: UITableView,heightForRowAt indexPath: IndexPath) -> CGFloat {
  3. return self.heightForBasicCell(at: indexPath)
  4. }
  5.  
  6.  
  7. func heightForBasicCell(at indexPath: IndexPath) -> CGFloat {
  8. var sizingCell: YourCell? = nil
  9. var onceToken: dispatch_once_t
  10. dispatch_once(onceToken,{() -> Void in
  11. sizingCell = tableView.dequeueReusableCell(withIdentifier: YourCell.cellIdentifier())!
  12. })
  13. self.configureBasicCell(sizingCell,at: indexPath)
  14. return self.calculateHeight(forConfiguredSizingCell: sizingCell)
  15. }
  16.  
  17. func calculateHeight(forConfiguredSizingCell sizingCell: YourCell) -> CGFloat {
  18. sizingCell!.bounds = CGRect(x: 0.0,y: 0.0,width: CGRectGetWidth(tableView.frame),height: CGRectGetHeight(sizingCell!.bounds))
  19. sizingCell!.setNeedsLayout()
  20. sizingCell!.layoutIfNeeded()
  21. var size = sizingCell!.contentView.systemLayoutSizeFittingSize(UILayoutFittingCompressedSize)
  22. return size.height + 1.0
  23. // Add 1.0f for the cell separator height
  24. }
  25.  
  26. func configureBasicCell(_ cell: YourCell,at indexPath: IndexPath) {
  27. //set your text here
  28. cell(“text”)
  29. }

根据您的需要更改代码.我刚刚将客观C代码转换为swift.

猜你在找的iOS相关文章