UITableViewCell的高度与UILabel自适应

UITableViewCell内部只放了一个UILabel,Cell的高度随着UILabel内容的高度变化而变化,可重写UITableView的委托方法动态调整高度,还要设置UILabel.numberOfLines = 0

 override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
if indexPath.row == 1 {
//If it's content cell
var boundSize = CGSizeMake(300, 500)
var text = NSString(string: lblContent.text!)
let attributes = NSDictionary(object: UIFont(name: "Arial", size: 15)!, forKey: NSFontAttributeName)
let size = text.boundingRectWithSize(boundSize, options: NSStringDrawingOptions.UsesLineFragmentOrigin, attributes: attributes as [NSObject : AnyObject], context: nil)
return size.height + 10
} else {
return 44
}
}
上一篇:新建项目虚拟环境及pycharm配置


下一篇:消除ExtJS6的extjs-trila字样