我有一个应用程序,其中包含多个UITableView控制器。在iOS 8.x上运行时,每个表中的所有单元格的高度都会根据单元格内容自动调整大小(所有单元格仅包含一个带有纯文本的UILabel)。现在在iOS 9上运行时,每个表中的每个单元格都只有一行高。这适用于动态和静态表格。我已经搜索了UIKit diffs文档并进行了广泛的搜索,但是我找不到正确的组合来获得除所有表中的所有单元格高度为一行之外的任何其他结果。
我有一个应用程序,其中包含多个UITableView控制器。在iOS 8.x上运行时,每个表中的所有单元格的高度都会根据单元格内容自动调整大小(所有单元格仅包含一个带有纯文本的UILabel)。现在在iOS 9上运行时,每个表中的每个单元格都只有一行高。这适用于动态和静态表格。我已经搜索了UIKit diffs文档并进行了广泛的搜索,但是我找不到正确的组合来获得除所有表中的所有单元格高度为一行之外的任何其他结果。
我遇到了类似的情况。诀窍似乎是通过UITableView
的代理方法明确实现动态大小调整。尽管在storyboards中设置为automatic
应该可以工作,但实际上并不行。解决方案是通过代理方法明确提供UITableViewAutomaticDimension
,然后像往常一样提供估计的单元格大小:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return UITableViewAutomaticDimension;
}
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
/* Return an estimated height or calculate
* estimated height dynamically on information
* that makes sense in your case.
*/
return 200.0f;
}
如果有人精确地知道为什么在iOS 9中这是必要的,以及它与iOS 8有何不同,我很想听听。
tableView.estimatedRowHeight = 60.0
tableView.rowHeight = UITableViewAutomaticDimension
在"cellForRowAtIndexPath"方法中,在返回cell之前添加以下代码。
cell.setNeedsUpdateConstraints()
cell.updateConstraintsIfNeeded()
对于iOS 9,我们可以添加tableView的委托方法:
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
}
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
iOS 9-10使用layoutMargins时存在UIStackView问题
extension UIStackView {
open override func didMoveToSuperview() { // a workaround for stackview issues on iOS 9-10
super.didMoveToSuperview()
if #available(iOS 11.0, *) { } else {
layoutMargins = self.layoutMargins
}
}
}
self.tableView.estimatedRowHeight = 100.0; self.tableView.rowHeight = UITableViewAutomaticDimension;
但是,我仍然没有找到修复具有静态单元格的表格的方法。 - ChiliOcean