Swift:无法以编程方式配置自定义表格单元格?初始化没有被调用? [英] Swift: can't configure custom table cell programmatically? Init not called?

查看:24
本文介绍了Swift:无法以编程方式配置自定义表格单元格?初始化没有被调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我正在尝试以编程方式将内容添加到我的自定义 tableview 单元格,如这里的 mob 上一个问题所示 - Swift:每次重新加载时都会一次又一次地添加表格单元格内容? 初始化 func tableView() 中的所有内容导致重叠.

Alright, I am trying to add content to my custom tableview cell programmatically as demonstrated in mob last question here - Swift: tableview cell content is added again and again with each reload? initializing all the content in func tableView() results in overlapping.

我已经逐字跟踪了这个问题 Swift.UITableViewCell 层次结构的正确初始化 在我的自定义单元格类(我在故事板中给它命名)中,我有:

I have followed this question verbatim Swift. Proper initialization of UITableViewCell hierarchy And in my custom cell class (which I give a name to in my storyboard) I have:

class EventTableCellTableViewCell: UITableViewCell {


    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier) // the common code is executed in this super call
        // code unique to CellOne goes here
        print("INIT")
        self.contentView.backgroundColor = UIColor.blackColor()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code

    }

这个 init 没有被调用,因为没有打印任何内容.错误出现在我的主 VC 中的 func tableView() 中.我原来有:

And this init is not called because nothing is printed. The errors come in my func tableView() in my main VC. I originally had:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("eventCell", forIndexPath: indexPath) as! EventTableCellTableViewCell
//        cell.eventTitle.text = names[indexPath.row]
//        cell.eventDescription.text = descriptions[indexPath.row]

        cell.contentView.clipsToBounds = false

        //cell UIX
        let eventTitleLabel = UILabel()
        let dateLabel = UILabel()
        let authorLabel = UILabel()
        let locationLabel = UILabel()

        let categoryView = UIImageView()
//then I add everything 

但这不起作用,所以我查看了其他帖子,现在有:

But this didn't work so I looked at other posts and now have:

 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        //var cell = tableView.dequeueReusableCellWithIdentifier("eventCell", forIndexPath: indexPath) as! EventTableCellTableViewCell

        var cell = tableView.dequeueReusableCellWithIdentifier("eventCell", forIndexPath: indexPath) as! UITableViewCell
        if (cell == nil) {
            cell = EventTableCellTableViewCell.init(style: .Default, reuseIdentifier: "eventCell")
        }

我也尝试在没有 indexPath 的情况下进行.现在我得到一个错误,cell cannot == nil,不管我写什么 init 都没有被调用.

I have also tried doing it without the indexPath. Right now I get an error that cell cannot == nil, and not matter what I write init is not called.

如何以编程方式配置我的单元?

How can I configure my cell programmatically?

推荐答案

如果单元格是在storyboard中设计的,只有init?(coder aDecoder: NSCoder)被调用,init(style:样式,reuseIdentifier: 永远不会被调用.

If the cell is designed in the storyboard only init?(coder aDecoder: NSCoder) is called, init(style: style, reuseIdentifier: is never called.

并且您必须在Interface Builder中将单元格的类设置为EventTableCellTableViewCell.

And you have to set the class of the cell in Interface Builder to EventTableCellTableViewCell.

这篇关于Swift:无法以编程方式配置自定义表格单元格?初始化没有被调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆