如何停止自动调整大小UITableViewCell的动画调整大小?

时间:2014-10-26 21:50:27

标签: ios iphone ios8 ios8.1

如果UITableViewController是显示具有可变单元格高度的UITableView的第一个视图,那么我看起来效果很好。但是,当我从一个表推送到另一个具有可变单元格高度的表时,相同的方法不起作用。

我在github上放了一个example。随意下载并搞砸它。

当您点击“我”时,表格会推送到另一个具有可变高度的单元格的表格视图。第二个表完成显示后,您可以看到单元格正在调整大小,特别是具有更多内容的单元格。在这种情况下,看到调整大小的现场很烦人。我希望在显示单元格之前正确调整其大小。

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

enter image description here

enter image description here

2 个答案:

答案 0 :(得分: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,则在显示第二个表时,我没有看到任何展开的行 - 仅在滚动或旋转之后。有了它,一切似乎都很好。

答案 1 :(得分:0)

将此添加到MyCell.swift似乎也消除了实时调整大小的问题。我不知道这与@rdelmar解决方案有什么不同,但这似乎也有效。

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

我在多行文字的内在内容大小下从Advanced Auto Layout Toolbox找到了这个答案。

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.
相关问题