I have a custom UITableViewCell
that only has one Label in it. I also set number of lines in Interface Builder to 0 as I read on StackOverflow.
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")
}
}
Here's the view controller's part to work with 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
}
So, .sizeToFit()
doesn't change anything—it still ellipsize the text on 1 line, and I need the label to be as many lines as it needed, and the size of the cell also be fitting for the label.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…