iOS 8 UITableView第一行的高度错误 [英] iOS 8 UITableView first row has wrong height

查看:114
本文介绍了iOS 8 UITableView第一行的高度错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个应用程序,我面临一个奇怪的问题。我在故事板中创建了一个UITableViewController,并添加了一个原型单元格。在这个单元格中,我添加了一个UILabel元素,这个UILabel占据了整个单元格。我已经使用自动布局进行了设置,并添加了左,右,顶部和底部约束。 UILabel包含一些文本。

I'm working on an app where I face a strange issue. I've created a UITableViewController in the storyboard and added a prototype cell. In this cell, I've added an UILabel element and this UILabel takes up the whole cell. I've set it up with Auto Layout and added left, right, top and bottom constraints. The UILabel contains some text.

现在在我的代码中,我初始化表视图的rowHeight和estimatedRowHeight:

Now in my code, I initialize the the rowHeight and estimatedRowHeight of the table view:

override func viewDidLoad() {
    super.viewDidLoad()

    self.tableView.rowHeight = UITableViewAutomaticDimension
    self.tableView.estimatedRowHeight = 50
}

我按如下方式创建单元格:

And I create the cell as follows:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    var cell : UITableViewCell? = tableView.dequeueReusableCellWithIdentifier("HelpCell") as? UITableViewCell
    if(cell == nil) {
        cell = UITableViewCell(style: .Default, reuseIdentifier: "HelpCell")
    }
    return cell!
}

我在表格视图中返回两行。这就是我的问题:第一行的高度是大的。似乎第二排,第三排等都具有正确的高度。我真的不明白为什么会这样。有人可以帮我吗?

I return two rows in my table view. Here comes my problem: the height of the first row is way to large. It appear that the second, third row etc all have a correct height. I really don't understand why this is the case. Can someone help me with this?

推荐答案

我遇到了第一次加载时单元格高度不正确的问题,但是在向上和向下滚动之后,单元格的高度是固定的。

I had a problem where the cells' height were not correct on the first load, but after scrolling up-and-down the cells' height were fixed.

我为这个问题尝试了所有不同的修复,然后最终发现调用这些函数之后最初调用 self.tableView.reloadData

I tried all of the different 'fixes' for this problem and then eventually found that calling these functions after initially calling self.tableView.reloadData.

            self.tableView.reloadData()
            // Bug in 8.0+ where need to call the following three methods in order to get the tableView to correctly size the tableViewCells on the initial load.
            self.tableView.setNeedsLayout()
            self.tableView.layoutIfNeeded()
            self.tableView.reloadData()

在初始加载后才进行这些额外的布局调用。

Only do these extra layout calls after the initial load.

我在这里找到了这个非常有用的信息:https://github.com/smileyborg/TableViewCellWithAutoLayoutiOS8/issues/10

I found this very helpful information here: https://github.com/smileyborg/TableViewCellWithAutoLayoutiOS8/issues/10

更新:
有时您可能还必须在 heightForRowAtIndexPath 中完全配置您的单元格,然后返回计算的单元格高度。请查看此链接以获取相关示例, http://www.raywenderlich .com / 73602 / dynamic-table-view-cell-height-auto-layout ,特别是 heightForRowAtIndexPath 上的部分。

Update: Sometimes you might have to also completely configure your cell in heightForRowAtIndexPath and then return the calculated cell height. Check out this link for a good example of that, http://www.raywenderlich.com/73602/dynamic-table-view-cell-height-auto-layout , specifically the part on heightForRowAtIndexPath.

更新2:我还发现非常有利于覆盖 estimatedHeightForRowAtIndexPath 并提供有些准确的行高估计。如果您有一个 UITableView ,其单元格可以是各种不同的高度,这将非常有用。

Update 2: I've also found it VERY beneficial to override estimatedHeightForRowAtIndexPath and supply somewhat accurate row height estimates. This is very helpful if you have a UITableView with cells that can be all kinds of different heights.

以下是 estimatedHeightForRowAtIndexPath 的实用示例实现:

Here's a contrived sample implementation of estimatedHeightForRowAtIndexPath:

public override func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {

    let cell = tableView.cellForRowAtIndexPath(indexPath) as! MyCell

    switch cell.type {
    case .Small:
        return kSmallHeight
    case .Medium:
        return kMediumHeight
    case .Large:
        return kLargeHeight
    default:
        break
    }
    return UITableViewAutomaticDimension
}

更新3: UITableViewAutomaticDimension 已针对iOS 9修复(woo-hoo!)。所以你的应该自动调整大小,而不必手动计算单元格高度。

Update 3: UITableViewAutomaticDimension has been fixed for iOS 9 (woo-hoo!). So you're cells should automatically size themselves without having to calculate the cells height manually.

这篇关于iOS 8 UITableView第一行的高度错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆