[英]Self Sizing Table View Cells isn't working
I set the estimatedRowHeight
to a constant and rowHeight
to UITableViewAutomaticDimension
but the table view cell is still not resizing to the detailTextLabel of my cell. 我设置了
estimatedRowHeight
常数,且rowHeight
至UITableViewAutomaticDimension
但表格视图单元格依然没有调整到我的手机的detailTextLabel。 Where am I going wrong? 我要去哪里错了?
override func viewDidLoad() {
super.viewDidLoad()
tableView.estimatedRowHeight = 130.0
tableView.tableFooterView = UIView()
tableView.separatorInset.left = 50
tableView.registerClass(CommentCellView.self, forCellReuseIdentifier: cellid)
tableView.rowHeight = UITableViewAutomaticDimension
}
override func viewDidAppear(animated: Bool) {
tableView.reloadData()
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier(cellid, forIndexPath: indexPath) as! CommentCellView
return cell
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 5
}
override func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 60.0
}
The UITableViewCell class: UITableViewCell类:
class CommentCellView: UITableViewCell {
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: .Subtitle, reuseIdentifier: reuseIdentifier)
detailTextLabel?.numberOfLines = 0
detailTextLabel?.text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
}
Set your label (called yourText
in this example) 设置标签(在此示例中称为
yourText
)
var yourText : UILabel = {
let label = UILabel()
label.translatesAutoresizingMaskIntoConstraints = false
return label
}()
Set your cell constraints in this way and put the setupComponents
method here: 以这种方式设置单元约束,然后在此处放置
setupComponents
方法:
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)!
setupComponents()
}
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: .Subtitle, reuseIdentifier: "CellId")
setupComponents()
}
func setupComponents(){
self.addSubview(yourText)
yourText.leftAnchor.constraintEqualToAnchor(self.leftAnchor, constant: 6).active = true
yourText.centerYAnchor.constraintEqualToAnchor(self.centerYAnchor).active = true
yourText.widthAnchor.constraintEqualToConstant(110).active = true
yourText.numberOfLines = 0
yourText.lineBreakMode = NSLineBreakMode.ByWordWrapping
self.bottomAnchor.constraintEqualToAnchor(yourText.bottomAnchor, constant: 5).active = true
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.