【发布时间】:2016-10-24 10:58:19
【问题描述】:
我有一个自定义UITableViewCell,其中只有一个标签。我还在 StackOverflow 上阅读时将 Interface Builder 中的行数设置为 0。
import UIKit
class CommonListViewCell: UITableViewCell {
@IBOutlet var background: UIView!
@IBOutlet var titleLabel: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
titleLabel.font = UIFont(name: "roboto", size: titleLabel.font.pointSize)
NSLog("CommonListViewCell font changed")
}
}
这是视图控制器与 UITableView 一起使用的部分:
override func viewDidLoad() {
super.viewDidLoad()
self.lvMain.delegate = self;
self.lvMain.dataSource = self;
self.lvMain.registerNib(UINib(nibName: "CommonListViewCell", bundle: nil), forCellReuseIdentifier: "commonlistidentifier")
self.lvMain.estimatedRowHeight = 100
self.lvMain.rowHeight = UITableViewAutomaticDimension
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("commonlistidentifier", forIndexPath: indexPath) as! CommonListViewCell
// Configure the cell...
cell.titleLabel?.text = prayerList[indexPath.row].name
cell.titleLabel?.sizeToFit()
setLabelFont(cell.titleLabel!)
Utils.nightCheck([cell.titleLabel!])
return cell
}
所以,.sizeToFit() 并没有改变任何东西——它仍然使 1 行上的文本椭圆化,我需要标签尽可能多的行,并且单元格的大小也适合标签。
【问题讨论】:
-
可以放标签截图吗?您想实现什么目标以及即将实现的目标?
-
您是否在标签的所有侧面都设置了自动布局约束?并且不要在标签上设置约束高度。
-
@Jecky 我想要一个文本是否为 4 行 - 看到所有内容并且单元格与标签的文本一样高,如果它是 1 行 - 为 1 行高。现在它是 1 行,并且行在末尾用“..”进行椭圆化
标签: ios swift uitableview uilabel