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

UPDATE

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

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

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

UITableView的定义如下

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

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

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

解决方法

我在项目中遇到了同样的问题.我通过以下方式克服了这一点,为行设置了高度.
//calculate the height by this way
    override func tableView(_ tableView: UITableView,heightForRowAt indexPath: IndexPath) -> CGFloat {
        return self.heightForBasicCell(at: indexPath)
    }


    func heightForBasicCell(at indexPath: IndexPath) -> CGFloat {
        var sizingCell: YourCell? = nil
        var onceToken: dispatch_once_t
        dispatch_once(onceToken,{() -> Void in
            sizingCell = tableView.dequeueReusableCell(withIdentifier: YourCell.cellIdentifier())!
        })
        self.configureBasicCell(sizingCell,at: indexPath)
        return self.calculateHeight(forConfiguredSizingCell: sizingCell)
    }

    func calculateHeight(forConfiguredSizingCell sizingCell: YourCell) -> CGFloat {
        sizingCell!.bounds = CGRect(x: 0.0,y: 0.0,width: CGRectGetWidth(tableView.frame),height: CGRectGetHeight(sizingCell!.bounds))
        sizingCell!.setNeedsLayout()
        sizingCell!.layoutIfNeeded()
        var size = sizingCell!.contentView.systemLayoutSizeFittingSize(UILayoutFittingCompressedSize)
        return size.height + 1.0
        // Add 1.0f for the cell separator height
    }

    func configureBasicCell(_ cell: YourCell,at indexPath: IndexPath) {
//set your text here
    cell(“text”)
    }

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

相关文章

当我们远离最新的 iOS 16 更新版本时,我们听到了困扰 Apple...
欧版/美版 特别说一下,美版选错了 可能会永久丧失4G,不过只...
一般在接外包的时候, 通常第三方需要安装你的app进行测...
前言为了让更多的人永远记住12月13日,各大厂都在这一天将应...