【问题标题】:how to adjust uilabel and uitableviewcell height programmatically?如何以编程方式调整 uilabel 和 uitableviewcell 高度?
【发布时间】:2017-02-22 22:43:12
【问题描述】:

我知道调整 UILabel 高度和 UITableViewCell 可能是一个非常标准的问题,但我找到了很多基于情节提要和检查器的答案,但不仅仅是使用 Swift 3。我创建了一个表覆盖屏幕。单元格的高度确定如下:

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {   
        return UITableViewAutomaticDimension
    }

我的 tableViewCell 中有几个对象,一个 UIImageView (myImage) 和一个 UILabel (myText),在自定义类中设置。定位和调整大小在 cellForRowAt 中进行。

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
    {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! CustomCell
        cell.myImage.image = UIImage(named: "MyImage")
        cell.myImage.layer.frame = CGRect(origin: CGPoint(x: 0, y: 10), size: (cell.myImage?.image?.size)!)
        cell.myText.text = myArray[indexPath.row]
        cell.myText.frame = CGRect(x: UIScreen.main.bounds.size.width * 0.25, y: 0, width: UIScreen.main.bounds.size.width * 0.7, height: 90)
        cell.myText.numberOfLines = 0
        return cell
    }

结果是一堆堆叠的单元格,彼此重叠。我应该怎么做才能将 UILabel 框架的高度调整为来自 myArray 的文本量并调整单元格高度,使其至少是 myImage 或 myText 的高度?

【问题讨论】:

  • 你用过自动布局吗?
  • 我正在使用自动布局,感谢您提供前面问题的链接。我错过了在另一个问题中以编程方式为多个对象设置约束的示例。约束是从概念上描述的,例如参考 updateConstraints(),或使用界面生成器显示。关于如何将 UILabel、UIImageViews 等对象与单元格的 contentView 以及如何/何时调用 updateConstraints 相关联,您是否有更多解释?
  • 如果我的评论或提供的链接对您有帮助,请点赞我的评论。谢谢

标签: uitableview swift3 uilabel


【解决方案1】:

您可以在 Swift 3 的表格视图中制作多行标签。

    import UIKit

        private let useAutosizingCells = true

        class TableViewController: UITableViewController {

            fileprivate let cellIdentifier = "Cell"

            // MARK: - Lifecycle

            override func viewDidLoad() {
                super.viewDidLoad()

                //setup initial row height here
                if useAutosizingCells && tableView.responds(to: #selector(getter: UIView.layoutMargins)) {
                    tableView.estimatedRowHeight = 44.0
                    tableView.rowHeight = UITableViewAutomaticDimension
                }
            }

    // MARK: - UITableViewDataSource
   extension TableViewController {

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

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath)
        let details = detailsModel[indexPath.row]

        cell.textLabel?.text = details.title
        cell.detailTextLabel?.text = details.description

        if useAutosizingCells && tableView.responds(to: #selector(getter: UIView.layoutMargins)) {
            cell.textLabel?.numberOfLines = 0
            cell.detailTextLabel?.numberOfLines = 0
        }

        return cell
    }

}

【讨论】:

  • 谢谢。我不明白为什么行数应该依赖于常量 useAutosizingCells。根据我目前所学到的,首先需要设置单元格中对象的约束,将单元格的 contentView 绑定到标签的下边缘。我正在研究它是如何完成的。你有一个代码示例来说明如何做到这一点吗?
猜你喜欢
  • 1970-01-01
  • 2013-01-09
  • 1970-01-01
  • 2010-11-03
  • 1970-01-01
  • 1970-01-01
  • 2012-08-13
  • 2020-01-11
  • 1970-01-01
相关资源
最近更新 更多