简体   繁体   English

如何在Swift中创建自定义UITableViewCell类?

[英]How do I create a custom UITableViewCell class in Swift?

I'm trying to follow along the following Using Auto Layout in UITableView for dynamic cell layouts & variable row heights top answer in Swift but for the IOS 7 version to mitigate some bugs in the IOS 8. 我正在尝试遵循以下使用UITableView中的Auto Layout在 Swift中动态单元格布局和可变行高的最佳答案,但对于IOS 7版本,它可以缓解IOS 8中的一些错误。

Right now, my custom cell class is: 现在,我的自定义单元格类是:

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
    }

}

In my Storyboard I set my custom class as the class for the cell. 在我的情节提要中,将自定义类设置为单元格的类。 I don't know if I have to use my reuseIdentifier anywhere. 我不知道是否必须在任何地方使用我的复用标识符。 I don't know how to get that working in the next piece of code. 我不知道如何在下一段代码中实现该功能。

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
}

I thought that would work, but when I call cell.name.text = post.name , I get an EXC_BAD_INSTRUCTION . 我以为这样可以,但是当我调用cell.name.text = post.name ,我得到了EXC_BAD_INSTRUCTION I'm guessing the cell isn't properly hooked up to the storyboard somehow or something. 我猜该单元未正确连接到情节提要。 How do I fix this? 我该如何解决?

The problem is that you don't understand how nibs work. 问题是您不了解笔尖的工作原理。 You have set up your PostCell to be configured when it is loaded from its nib: 您已设置要从笔尖加载PostCell的PostCell:

class PostCell: UITableViewCell {
    @IBOutlet var name: UILabel!
}

So that means that name is nil - until this cell is instantiated from the nib, at which time the outlet will be hooked up. 因此,这意味着namenil直到从笔尖实例化此单元格为止,此时将连接出口。 But then later you say: 但是后来您说:

var cell = PostCell()

But that is the wrong PostCell! 但是, 是错误的PostCell! You didn't load it from the nib; 没有从笔尖加载它; you just made one out of whole cloth. 您只是用整块布做成的。 So since there was no nib-loading, the outlet (as you rightly suspect) was naturally never connected, and so cell.name is nil . 因此,由于没有笔尖加载,因此插座(如您所理所当然的)自然不会连接,因此cell.namenil

So the solution is: do not make one out of whole cloth. 所以,解决的办法是: 不要让一个出全棉布的。 Load it from the nib. 从笔尖加载它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM