gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 03:19:13 25 4
gpt4 key购买 nike

更新

我知道是什么导致了这种奇怪的布局中断。它是一个附件(UITableViewCellAccessory)的设置。如果我停止指定配件,布局不会中断。我没有添加这个作为答案,因为答案需要一个解决方案,在不破坏布局的情况下给我一个附件

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

进一步旋转或进一步滚动将使行回到预期高度。我已经包含了一些之前和之后的截图

Table before scroll

Table after scroll and rotate

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
};

请注意,通常怀疑设置了 RowHeightEstimatedRowHeight。一旦我从标签中删除 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”)
}

根据您的需要更改代码。我刚刚将 objective-c 代码转换为 swift。

关于ios - 可变高度 UITableViewCell 在滚动到列表末尾并旋转后展开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39466259/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com