【发布时间】:2015-12-18 04:29:25
【问题描述】:
我试图弄清楚如何为包含 uitextview 的 tableview 单元格获取正确的高度,以便显示 uitextview 中的所有内容。 textview 不可编辑且不可滚动,这意味着 uitextview 的字符串是已知的。我尝试简单地返回 heightForRowAtIndexPath 中故事板中的 tableview 单元格的高度,但如果它太大,则会切断内容。我还尝试计算 heightForRowAtIndexPath 中 textView 的高度,但它在运行时抛出错误:
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
let cellInfo = UserCellsArray[indexPath.section]
switch cellInfo {
case .userInfo: return 104
case .ubio:
let cell = tableView.dequeueReusableCellWithIdentifier(cellInfo.description, forIndexPath: indexPath) as! UserProfileTableViewCell //error
let oldHeight = cell.userBio.frame.size.height
cell.userBio.text = self.bio
var bodyframe = cell.userBio.frame
bodyframe.size = cell.userBio.contentSize
let newHeight = cell.userBio.contentSize.height
return tableView.rowHeight - oldHeight + newHeight
}
}
【问题讨论】:
-
您使用 TextView 而不是 UILabel 的任何具体原因?
-
@Shripada 除了我不确定如何在 tableviewcell 中使用自动布局格式化它之外,考虑到标签中会添加大量文本。如果它使问题更容易,我可以轻松切换到标签。
-
是的,你可以有一个可调整大小的 UILabel,使用 UITableView 的约束和一些设置,我会尝试在一段时间内发布答案
标签: ios swift uitableview swift2