UITableViewCell中的UIStackView和多行标签 [英] UIStackView and multi-line labels in a UITableViewCell

查看:210
本文介绍了UITableViewCell中的UIStackView和多行标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个自定义UITableViewCell,其中包含许多垂直堆叠的UILabel,所有这些都可以包含换行的文本.有了iOS9中新的UIStackView和单元格自动调整大小机制,我认为这很简单,但是我无法使其正常工作.我正在使用Xcode 7beta3.

I want to have a custom UITableViewCell with a number of vertically stacked UILabels all of which can contain text that wraps multiple lines. With the new UIStackView in iOS9 and the cell self-sizing mechanism I thought this would be straightforward, but I can't get it to work. I am using Xcode 7beta3.

我创建了一个简单的测试应用.这是IB中的设置:

I created a simple test app. This is the setup in IB:

UIStackViewUIStackViewDistribution.FillUIStackViewAlignment.Fill是垂直的,但我基本上已经尝试了所有组合,但均未成功.

The UIStackView is vertical with UIStackViewDistribution.Fill and UIStackViewAlignment.Fill but I have basically tried all the combinations without success.

UILabel具有numberOfLines = 0lineBreakMode = .ByWordWrapping.

代码:

class Cell: UITableViewCell {

    @IBOutlet var label1: UILabel!
    @IBOutlet var label2: UILabel!
}

class MasterViewController: UITableViewController {

    struct Data {
        let label1: String
        let label2: String
    }

    var objects = [Data]()

    override func viewDidLoad() {
        super.viewDidLoad()

        tableView.rowHeight = UITableViewAutomaticDimension
        tableView.estimatedRowHeight = 60

        createData()
    }

    private func createData() {

        for ii in 0 ..< 20 {

            let data = Data(label1: "\(ii): hello world this is a longer string that should wrap without going weird", label2: "label2 has lots of writing as well and so should wrap onto multiple lines")
            objects.append(data)
        }
    }

    // MARK: - Table View

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }

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

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! Cell
        let data = objects[indexPath.row]

        cell.label1?.text = data.label1
        cell.label2?.text = data.label2

        return cell
    }
}

我想知道Beta中是否存在错误,因为有时其中一个标签会自动换行,而另一个标签不会自动换行.

I wonder if there is a bug in the beta, because sometimes one of the labels wraps while the other doesn't.

例如,我尝试将UIStackView嵌套在另一个UIStackView中,如下所示:

For example I tried nesting the UIStackView in another UIStackView as follows:

我完全没有更改代码.现在,一个标签会自动换行,而另一个则不会:

I made no code changes at all. Now one label wraps while the other does not:

任何帮助将不胜感激!

推荐答案

问题是在单元格初始化时,单元格的宽度默认为600,因此它将基于600宽度计算行的高度.这是因为单元是在情节提要中使用大小类创建的.

The problem is at the time of cell initialisation the width of the cell will be 600 by default so it will calculate the height of the row based on 600 width. This is because cell is created in the storyboard with size classes.

我认为这将是beta版中的一个错误,作为解决方案,请在xib中使用您的单元格,然后使用.它将正常工作.

i think it will be a bug in the beta version and as a solution of this take your cell in xib and then use . It will work fine.

这篇关于UITableViewCell中的UIStackView和多行标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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