在iOS 9上,所有的uitableviewcell都变成了一行高度。

4

我有一个应用程序,其中包含多个UITableView控制器。在iOS 8.x上运行时,每个表中的所有单元格的高度都会根据单元格内容自动调整大小(所有单元格仅包含一个带有纯文本的UILabel)。现在在iOS 9上运行时,每个表中的每个单元格都只有一行高。这适用于动态和静态表格。我已经搜索了UIKit diffs文档并进行了广泛的搜索,但是我找不到正确的组合来获得除所有表中的所有单元格高度为一行之外的任何其他结果。


2
我通过在viewDidLoad中添加以下内容成功修复了具有动态单元格的表格:self.tableView.estimatedRowHeight = 100.0; self.tableView.rowHeight = UITableViewAutomaticDimension;但是,我仍然没有找到修复具有静态单元格的表格的方法。 - ChiliOcean
出于某些原因,在Storyboard中设置numberOfLines = 0无法生效。在cellForRowAtIndexPath中设置它可以正确地得到高度。此外,调用代理estimatedRowHeight有助于边距更加美观。 - runios
3个回答

11

我遇到了类似的情况。诀窍似乎是通过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有何不同,我很想听听。


1
我们可以在iOS 8中使用"UITableViewAutomaticDimension"来实现动态尺寸,只需使用以下两个属性:-
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
}

0

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接