繁体   English   中英

如何更改表格行的高度以适合UILabel

[英]How to change height of table row to fit UILabel


我正在编写一个消息传递应用程序,它具有一个在两个用户之间输出消息的表视图。 我遇到的问题是调整表行的大小以适合整个UILabel。 截至目前,我还没有连接数据库来显示任何消息,只是预定义的字符串充当虚假消息,以确保表行的大小正确调整。
我有一个确定文本标签所需高度的函数,但是我无法使每行调整到此约束的大小。 感谢您的时间
码:

//
//  chatViewController.swift
//  collaboration
//
//  Created by nick on 11/21/15.
//  Copyright © 2015 Supreme Leader. All rights reserved.
//

import UIKit

class chatViewController: UIViewController {

@IBOutlet weak var tableView: UITableView!

@IBOutlet weak var testLabel: UILabel!

var messages: NSMutableArray = NSMutableArray()

func tableView(tableView: UITableView, numberOfRowsInSection Section: Int) -> Int {
    return self.messages.count
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = self.tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! chatTableViewCell
    cell.messageLabel?.text = self.messages.objectAtIndex(indexPath.row) as? String
    print(requiredHeight(self.messages.objectAtIndex(indexPath.row) as! String))

    tableView.estimatedRowHeight = requiredHeight(self.messages.objectAtIndex(indexPath.row) as! String)
    tableView.rowHeight = UITableViewAutomaticDimension

    return cell
}

func requiredHeight(text:String) -> CGFloat {
    let font = UIFont(name: "Helvetica", size: 16.0)
    let label:UILabel = UILabel(frame: CGRectMake(0, 0, 200, CGFloat.max))
    label.numberOfLines = 0
    label.lineBreakMode = NSLineBreakMode.ByWordWrapping
    label.font = font
    label.text = text
    label.sizeToFit()
    return label.frame.height
}

override func viewDidLoad() {
    super.viewDidLoad()
    let receivedUsername = NSUserDefaults.standardUserDefaults().stringForKey("usernameToMessageWith")
    testLabel.text = "\(receivedUsername! as String)"
    messages.addObject("dfsdfdfsdfsdfsdf")
    messages.addObject("You: and i i iiiknkjdf lorem ipsum i really like economics class because i can program as opposed to actually doing work")
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()

}

}

我强烈建议您使用xib文件并在那里设置自动布局。 然后,您的估计身高将按预期工作。

除此之外,您可以简单地返回计算出的高度,以heightForRowAtIndexPath形式调用它。 这样,您将为每个单元格使用单独的高度。 例如:

override func tableView(tableView: UITableView!, heightForRowAtIndexPath indexPath: NSIndexPath!) -> CGFloat {
        return requiredHeight(self.messages.objectAtIndex(indexPath.row) as! String)
}

同样,我会先尝试自动布局。

我希望我能正确理解。 查看您的代码后,我建议以下内容:

使用自动版式。

设置与uitableviewcell相关的尾随/前导和顶部/底部约束的子视图。

然后,可以通过激活一次使用Apple的AutomaticDimension功能。

override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(animated)

    self.tableView.estimatedRowHeight = 44.0
    self.tableView.rowHeight = UITableViewAutomaticDimension
    self.tableView.reloadData()
}

不要在原型单元中使用自定义行高功能。 不要自动计算行高。 一次将rowHeight设置为UITableViewAutomaticDimension,而不是在cellForRowAtIndexPath方法中。

一切都应该正常工作。

根据您的评论,解决方案的其他步骤:请检查:UILabel属性:“行”应设置为“ 0”,“换行符”应设置为“自动换行”

在情节提要表视图单元格检查器中,您可以看到单元格行高是固定的。 因此,在视图中确实已加载,将情节提要中的估计行高设置为1,并设置行高以自动获得其高度。 同时检查情节提要中的标签,并将“行数”设置为0以显示多行。

override func viewDidLoad() {
     super.viewDidLoad()

     tableView.estimatedRowHeight = tableView.rowHeight
     tableView.rowHeight = UITableViewAutomaticDimension
}

您应该通过情节提要或以编程方式将UILayout的自动布局约束应用于UILabel 设置约束为零 还将宽高比应用于UILabel。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM