【发布时间】:2016-10-29 16:34:01
【问题描述】:
在我的UITableViewController 中,我有许多自定义单元格,其高度取决于其中的 UITextView 的长度。这意味着我无法从 heightForRowAtIndexPath() 方法返回精确的大小,因为那时我仍然不知道。此外,由于我无法为支配UITextView 的约束创建@IBOutlet weak var,因此这是我尝试过的代码:
let cell = tableView.dequeueReusableCellWithIdentifier("BigTextCell", forIndexPath: indexPath) as! BigTextCell
cell.backgroundColor = UIColor(patternImage: UIImage(named: "icons/background.png")!)
cell.paragraph.layer.borderColor = UIColor(red: 0/255, green: 0/255, blue: 0/255, alpha: 1).CGColor
cell.paragraph.layer.borderWidth = 0.5
cell.paragraph.editable = false
cell.title.enabled = false
switch (indexPath.row) {
case 1:
cell.title.text = "Chi è:"
cell.icon.image = UIImage(named: "icons/field_fish.png")
cell.paragraph.text = self.fieldsCollection["life_method"] as! String
cell.paragraph.backgroundColor = UIColor(patternImage: UIImage(named: "icons/quadretti.png")!)
for constraint in cell.icon.constraints {
if (constraint.identifier == "icon_width") {
constraint.constant = 64.0
break
}
}
for constraint in cell.paragraph.constraints {
if (constraint.identifier == "longTextConstraint") {
constraint.constant = cell.paragraph.sizeThatFits(CGSize(width: cell.paragraph.frame.width, height: CGFloat.max)).height
break
}
}
...
因此,UITextView 根据文本的长度正确调整大小,但通常单元格的高度太大。我怎样才能解决这个问题?
谢谢
更新
正如你们中的一些人所建议的,如果我在heightForRowAtIndexPath 中尝试这个
if (indexPath.row == 1) {
//return 235.0
let cell = tableView.dequeueReusableCellWithIdentifier("BigTextCell", forIndexPath: indexPath) as! BigTextCell
cell.paragraph.text = self.fieldsCollection["life_method"] as! String
for constraint in cell.paragraph.constraints {
if (constraint.identifier == "longTextConstraint") {
constraint.constant = cell.paragraph.sizeThatFits(CGSize(width: cell.paragraph.frame.width, height: CGFloat.max)).height
return constraint.constant+30.0
break
}
}
}
模拟器方块。
【问题讨论】:
标签: ios xcode swift uitableview