1

如果 UITableViewController 是第一个显示具有可变单元格高度的 UITableView 的视图,那么我看过的所有示例都运行良好。但是,当我从一张桌子推到另一张桌子时,相同的方法不起作用,该桌子具有可变的单元格高度。

我在github上举了一个例子。随意下载并弄乱它。

当您点击 Click me 时,表格会推送到另一个表格视图,其中包含可变高度的单元格。一旦第二个表格完成显示,您就可以看到单元格正在调整大小,尤其是具有更多内容的单元格。在这种情况下,实时看到调整大小很烦人。我希望单元格在显示之前正确调整大小。

有没有人有类似的问题或解决方案?

在此处输入图像描述

在此处输入图像描述

4

2 回答 2

0

Adding this to MyCell.swift seems to eliminate the live resizing issue as well. I don't know how this is really different from @rdelmar solution, but this seems to work as well.

override func layoutSubviews() {
    super.layoutSubviews()
    self.titleLabel.preferredMaxLayoutWidth = self.titleLabel.frame.size.width
    super.layoutSubviews()
}

I found this answer from Advanced Auto Layout Toolbox under Intrinsic Content Size of Multi-Line Text.

Since we usually don’t know this value in advance, we need to take a two-step approach to
get this right. First we let Auto Layout do its work, and then we use the resulting frame
in the layout pass to update the preferred maximum width and trigger layout again.
于 2014-10-26T22:34:12.107 回答
0

您不需要 cellForRowAtIndexPath 中的某些代码,最好在 IB 或单元代码中设置 numberOfLines。我将 cellForRowAtIndexPath 更改为如下,并在单元格中添加代码如下,

在 cell.m 中,

override func didMoveToSuperview() {
    self.titleLabel.numberOfLines = 0
    self.subTitleLabel.numberOfLines = 0
    self.layoutIfNeeded()
}

在 cellForRowAtIndexPath 中,

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as MyCell
        let content = self.data[indexPath.row]
        cell.titleLabel.text = content.line1
        cell.subTitleLabel.text = content.line2
        return cell;
    }

如果单元格代码中没有 layoutIfNeeded,当显示第二个表格时,我没有看到任何扩展行——仅在滚动或旋转之后。有了它,一切似乎都很好。

于 2014-10-26T22:08:53.953 回答