无法以编程方式创建自定义表格视图单元格,Swift

时间:2017-07-01 01:56:17

标签: swift uitableview tableviewcell

我已按照此stackoverflow question & answer作为指导在表格视图中创建自定义单元格(不在故事板中添加原型单元格),但是我无法使用自定义单元格创建表格视图,我只是得到一排空单元格。我确实看过其他SO问题,但他们没有帮助。

我相信我注册单元格的方式有问题。 (仅供参考:当我将一个原型单元格添加到我的表视图并为其分配一个类和单元​​格ID时,如果我然后禁用tableView.register(AppointmentTVCell.self, forCellReuseIdentifier: cellId)它可以工作,我对此不感兴趣,因为我想最小化IB的使用)

下面是我在storyboard中的tableViewController,已经为其分配了一个表视图类AppointmentsTVC enter image description here

以下是我的表视图控制器代码

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

以下是自定义表格视图单元格的代码

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

2 个答案:

答案 0 :(得分:1)

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

}

答案 1 :(得分:0)

我已经知道,如果没有NIB文件,将不会调用awakeFromNib,因此未添加视图。

通过查看此Stackoverflow question,我将代码修改为以下内容并且有效。

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