无法以编程方式创建自定义 Table 查看单元格,Swift

Unable To Create a Custom Table View Cell programatically, Swift

我按照这个 Whosebug question & answer 作为指南在 table 视图中创建自定义单元格(没有在情节提要中添加原型单元格),但是我无法创建 table 查看自定义单元格,我只得到空单元格行。我确实看过其他 SO 问题,但它们没有帮助。

我认为我注册单元格的方式有问题。 (仅供参考:当我将原型单元格添加到我的 table 视图并为其分配一个 class 和单元格 ID,然后如果我禁用 tableView.register(AppointmentTVCell.self, forCellReuseIdentifier: cellId) 它会起作用,我对此不感兴趣,因为我希望尽量减少 IB 的使用)

下面是我在故事板中的 tableViewController,已为其分配了 table 视图 class AppointmentsTVC

下面是我的 Table View Controller

代码
class AppointmentsTVC: UITableViewController {

    let cellId = "reuseIdentifier"

    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.register(AppointmentTVCell.self, forCellReuseIdentifier: cellId)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // MARK: - Table view data source

    override func numberOfSections(in tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return 2
    }


    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        print("Cell for Row at func called")
        let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as! AppointmentTVCell  // Creating a cell
        cell.caseId.text = "123456"
        return cell
    }

    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 150.0
    }
}

下面是自定义 table 视图单元格的代码

class AppointmentTVCell: UITableViewCell {

    let caseId: UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFont(ofSize: 14)
        label.frame = CGRect(x: 0, y: 50, width: 100, height: 30)
        label.numberOfLines = 2
        //label.text = "Hello"
        label.textColor = UIColor.black
        return label
    }()

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        contentView.addSubview(caseId)

    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
}

使用 didMoveToSuperview 方法

class AppointmentTVCell: UITableViewCell {

let caseId: UILabel = {
    let label = UILabel()
    label.font = UIFont.systemFont(ofSize: 14)
    label.frame = CGRect(x: 0, y: 50, width: 100, height: 30)
    label.numberOfLines = 2
    //label.text = "Hello"
    label.textColor = UIColor.black
    return label
}()
override func didMoveToSuperview() {
    super.didMoveToSuperview()
    if superview != nil {
    contentView.addSubview(caseId)
    }
}

override func setSelected(_ selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)

    // Configure the view for the selected state
}

}

我了解到如果没有 NIB 文件,awakeFromNib 将不会被调用,因此不会添加视图。

通过查看此 我将代码修改为以下并且它起作用了。

class AppointmentTVCell: UITableViewCell {

    var caseId = UILabel()

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


    override init(style: UITableViewCellStyle, reuseIdentifier: String!) {

        super.init(style: style, reuseIdentifier: reuseIdentifier)
        //Do your cell set up
        caseId.frame = CGRect(x: 0, y: 50, width: 100, height: 30)
        caseId.text = "Hello"
        caseId.font = UIFont(name:"HelveticaNeue-Bold", size: 16)
        contentView.addSubview(caseId)
    }


    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
    }
}