我的表格中间有一些行需要根据内容调整高度。 Detail 标签可以有很长的 CSV 值列表,我的尝试如下:
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
if(indexPath.section == 0){
return 176;
}
if(indexPath.section == 1){
if let cell = tableView.cellForRowAtIndexPath(indexPath) {
cell.detailTextLabel!.frame.size = CGSize(width: view.bounds.size.width - 115, height: 10000)
cell.detailTextLabel!.sizeToFit()
cell.detailTextLabel!.frame.origin.x = view.bounds.size.width - cell.detailTextLabel!.frame.size.width - 15
return cell.detailTextLabel!.frame.size.height + 20
}
}
return 44;
}
但是我在以下位置收到 EXEC_BAD_ACCESS 错误:
if let cell = tableView.cellForRowAtIndexPath(indexPath)
关于为什么我会收到此错误或解决此问题的正确方法有什么想法吗?
最佳答案
您正试图获取一个尚不存在的单元格。 cellForRowAtIndexPath(_ indexPath: NSIndexPath) -> UITableViewCell?
返回的单元格是您在 tableView(_ tableView: UITableView,
cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
。因此,您试图通过获取单元格(同样,尚不存在)来计算尚不存在的单元格的高度。看起来你想要的是制作一个假的占位符单元格的原型(prototype),你可以设置它来获得真实单元格的高度。为此,您可以将单个单元格作为委托(delegate)/数据源类的属性,设置该单元格一次,然后返回其高度,就像您在高度方法中尝试做的那样。
关于ios - 快速动态的 UITableView 行高,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32060389/