获取自定义单元格中标签的高度以用于主视图控制器中的 heightForRowAt [英] getting the height of a label in custom cell to use for heightForRowAt in my main view controller
问题描述
我有一个视图控制器,它使用一个名为 searchCell 的自定义单元格来引入内容,我想知道如何获取标签 lblLeft 的高度并在 tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) 中使用它-> CGFloat 函数返回标签的高度.我在主视图控制器中使用的代码:
I have a view controller that uses a custom cell called searchCell to bring content in and I am wondering how I can get the height of the label lblLeft and use it in the tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat function to return the height of the label. The code I'm using in my Main View Controller:
func tableView(_ tableView: UITableView, cellForRowAt indexPath:
IndexPath) -> UITableViewCell {
if searchMode == searching {
let cell: MHSearchCell = tableView.dequeueReusableCell(withIdentifier:
MHSearchCell.ReusableIdentifier()) as! MHSearchCell
cell.helper = helpers[indexPath.row]
cell.delegate = self
return cell
}
}
在我的 MHSearchCell 中创建标签的部分代码,标签在这里作为 IBOutlet 连接:
Part of the code that creates the label in my MHSearchCell, the label is connected here as an IBOutlet:
lblLeft.text = ""
if let expertiseCount = helper.expertise {
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.lineSpacing = 5
paragraphStyle.lineBreakMode = .byWordWrapping
paragraphStyle.alignment = NSTextAlignment.center
lblLeft.numberOfLines = 0
let finalString = expertiseCount.map({$0.name!}).joined(separator: " \u{00B7} ")
let finalAttributedString = NSMutableAttributedString(string: finalString, attributes: [NSParagraphStyleAttributeName:paragraphStyle])
lblLeft.attributedText = finalAttributedString
}
推荐答案
第一种方法要么使用自动尺寸标注
1) 对标签应用适当的约束检查下图.
1) Apply Appropriate constraints on label check the image below.
确保故事板中的标签 Lines 设置为 0
.
Make sure label Lines are set to 0
in storyboard.
现在在控制器类中执行此操作-:
Now in controller class do this-:
//MArk-: ViewDidLoad
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
yourTableView.estimatedRowHeight = 142.0 // Default cell height from storyboard
yourTableView.rowHeight = UITableViewAutomaticDimension
}
表格功能-:
public func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat{
return UITableViewAutomaticDimension
}
另一种计算estimatedHeightForLabel-的方法:
1) 对标签应用与上述相同的约束
1) Apply same constraints to label as mentioned above
2) 使用 NSString 的 boundingRect
方法从标签中找到单元格的准确高度 -:
2) use boundingRect
method of NSString to find exact height for cell from label -:
表格视图功能-:
extension ViewController : UITableViewDelegate,UITableViewDataSource{
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
return value.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as? testingCell
cell?.textlabel.text = value[indexPath.row]
return cell!
}
// Number of sections in table
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}// Default is 1 if not implemented
public func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat{
let text = value[indexPath.row]
let estimatedheight = calculateEstimatedHeight(text: text)
return estimatedheight.height
}
func calculateEstimatedHeight(text:String) -> CGRect{
let size = CGSize(width: view.frame.width, height: 1000)
let options = NSStringDrawingOptions.usesFontLeading.union(.usesLineFragmentOrigin)
let attribute = [NSFontAttributeName:UIFont.systemFont(ofSize: 17)]
let estimatedHeight = NSString(string: text).boundingRect(with: size, options: options, attributes: attribute, context: nil)
return estimatedHeight
}
记住-:
1) 提供与标签相同的字体大小.
1) Provide same font size you have for label.
2) 对于多行标签使用选项为 .usesLineFragmentOrigin
.
2) For multiline label use option as .usesLineFragmentOrigin
.
CustomCellClass-:
import UIKit
class testingCell: UITableViewCell {
@IBOutlet weak var textlabel: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
这篇关于获取自定义单元格中标签的高度以用于主视图控制器中的 heightForRowAt的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!