我正在尝试遵循以下使用UITableView中的Auto Layout在Swift中动态单元格布局和可变行高的最佳答案,但对于IOS 7版本,它可以缓解IOS 8中的一些错误。
现在,我的自定义单元格类是:
class PostCell: UITableViewCell {
@IBOutlet var name: UILabel!
@IBOutlet var timestamp: UILabel!
@IBOutlet var postBody: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
在我的情节提要中,我将自定义类设置为单元格的类。我不知道是否必须在任何地方使用我的复用标识符。我不知道如何在下一段代码中实现该功能。
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
var cell = PostCell()
var post = self.posts[indexPath.row]
cell.name.text = post.name
cell.timestamp.text = post.timestamp
cell.postBody.text = post.body
cell.setNeedsUpdateConstraints()
cell.updateConstraintsIfNeeded()
cell.bounds = CGRectMake(0.0, 0.0, CGRectGetWidth(feed.bounds), CGRectGetHeight(cell.bounds))
cell.setNeedsLayout()
cell.layoutIfNeeded()
var height = cell.contentView.systemLayoutSizeFittingSize(UILayoutFittingCompressedSize).height
height += 1.0
return height
}
我以为可以,但是当我打电话时cell.name.text = post.name
,我得到了一个提示EXC_BAD_INSTRUCTION
。我猜该单元未正确连接到情节提要。我该如何解决?
问题是您不了解笔尖的工作原理。您已设置要从笔尖加载PostCell的PostCell:
class PostCell: UITableViewCell {
@IBOutlet var name: UILabel!
}
这样就意味着name
是nil
-直到这个小区从笔尖,届时出口将被迷上了实例化。但是后来您说:
var cell = PostCell()
但是,这是错误的PostCell!您没有从笔尖加载它;而是从笔尖加载了它。你只是用整块布做成的。因此,由于没有笔尖加载,所以插座(如您所理所当然的)自然不会连接,所以cell.name
也没有连接nil
。
因此,解决办法是:不要不使整个布的一出。从笔尖加载它。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句