【发布时间】:2016-03-20 14:38:41
【问题描述】:
我有一个带有多个 customCells 的 tableView,因此我不能使用 estimatedRowHeight。但是在这个单元格中,我需要将行高设置为单元格内标签的内容。但是它似乎没有显示所有的文本。
我从设置 cosntraints 开始
然后在单元格子类中我设置字体高度
descLabel!.font = UIFont.systemFontOfSize(14)
然后我创建计算函数
func calculateHeightForString(inString:String) -> CGFloat
{
let messageString = inString
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.lineSpacing = 2
let attributes = [NSFontAttributeName: UIFont.systemFontOfSize(14.0)]
let attrString:NSMutableAttributedString? = NSMutableAttributedString(string: messageString, attributes: attributes)
attrString!.addAttribute(NSParagraphStyleAttributeName, value:paragraphStyle, range:NSMakeRange(0, attrString!.length))
let rect:CGRect = attrString!.boundingRectWithSize(CGSizeMake(self.view.frame.width-24,CGFloat.max), options: NSStringDrawingOptions.UsesLineFragmentOrigin, context:nil )//hear u will get nearer height not the exact value
let requredSize:CGRect = rect
return requredSize.height //to include button's in your tableview
}
设置第 1 部分的高度
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
if indexPath.section == 0 {
return 160.0/320.0 * tableView.bounds.width
} else if indexPath.section == 1 {
return self.calculateHeightForString("Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also th") + 24
} else {
return 100
}
}
没有显示所有内容的结果
【问题讨论】:
-
检查 1 衬里的单元格高度是多少。如果与上面相同,则约束似乎不正确。
标签: ios swift uitableview