动态UITableCellView高度 [英] Dynamic UITableCellView height

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

问题描述

我按照本教程启用动态单元格高度: http://www.appcoda.com / self-sizing-cells /

I followed this tutorial to enable dynamic cell height: http://www.appcoda.com/self-sizing-cells/

所以我添加了

tableView.estimatedRowHeight = 44.0
tableView.rowHeight = UITableViewAutomaticDimension

in viewDidLoad ,将标签设置为0行并将字体设置为System。结果是单元格的大小现在是动态的,内容显示正确但不是在首次显示表格时。好吧,有些单元格正确显示,但其他单元格高度为44.当我滚动表格时,高度似乎得到纠正。在教程中,他们描述如下:

in viewDidLoad, set labels to 0 Lines and font set to System. The result is that the size of the cell is now dynamic and content is shown correctly but NOT when the table is first displayed. Well some cells are shown correctly but others have a height of 44. When I scroll the table the height seem to be corrected. In the tutorial they describe it like this:


首次显示表视图时,您可能会发现一些单元格
尺寸不合适。但是当您滚动表视图时,将显示具有正确行高的新
单元格。要解决此问题,
你可以在视图出现后强制重新加载

When the table view is first displayed, you may find some of the cells are not sized properly. But when you scroll the table view, the new cells are displayed with correct row height. To workaround this issue, you can force a reload after the view appears

我试过

override func viewDidLoad() {
    super.viewDidLoad()

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

override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(animated)
    self.tableView.reloadData()
}

但它不起作用。任何想法如何解决这一问题?我在xcode 6.1和iOS8.1上,我正在使用swift。

but it isn't working. Any ideas how to fix this? I am on xcode 6.1 and iOS8.1 and I am using swift.

我在github上创建了一个简单的测试项目: https://github.com/ArtworkAD/DynamicCellTest

I created a simple test project on github: https://github.com/ArtworkAD/DynamicCellTest

推荐答案

我下载了你在GitHub上创建的项目并进行了一些更改并使其正常工作。

I downloaded the project you created on GitHub and made a few changes and got it working.

ViewController中的代码看起来像这样......

The code in the ViewController looks like this...

import UIKit

class MyTableViewController: UITableViewController {

    var entries:Array<String> = [String]()

    override func viewDidLoad() {
        super.viewDidLoad()

        var i = 0
        while i < 20 {
            entries.append("\(i) Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor")
            i++;
        }

        self.tableView.rowHeight = UITableViewAutomaticDimension
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.entries.count
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        //ask for a reusable cell from the tableview, the tableview will create a new one if it doesn't have any
        let cell = self.tableView.dequeueReusableCellWithIdentifier("basic_cell", forIndexPath: indexPath) as UITableViewCell

        var label = cell.viewWithTag(13)

        if let unwrappedLabel = label as? UILabel {
            unwrappedLabel.text = self.entries[indexPath.row]
        }

        return cell
    }
}

故事板看起来像这样......

And the Storyboard looks like this...

请注意AutoLayout约束从上到下我将行数设置为0。

Notice the AutoLayout constraints going from top to bottom of the cell and I set the number of lines to 0.

然后在运行应用程序时......

Then when running the app...

这篇关于动态UITableCellView高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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