Swift custom UITableViewCell label is always nil

2020-05-30 03:26发布

I've been stuck with this problem for days, so I'd be really happy if someone could help. I'm trying to create a dynamic UITableView, for which I created a custom UITableView subclass and I've created a custom UITableViewCell subclass as well, because I need several UILabels and a UIButton in each cell. The cell is created, but the problem is that the value of the labels is always nil, hence the cell isn't displayed properly. This is, how the storyboard looks like, and this is what I see while running the program.

Here's my UITableViewCell subclass:

import UIKit

class QuestionTableViewCell: UITableViewCell {

    @IBOutlet var student: UILabel!
    @IBOutlet var labDesk: UILabel!
    @IBOutlet var topic: UILabel!
    @IBOutlet var answers: UILabel!

}

and my UITableView subclass:

import UIKit

class QuestionViewController: UITableViewController, UITableViewDelegate, UITableViewDataSource {

    @IBOutlet var table: UITableView!
     struct Question {
        var student: String
        var labDesk: String
        var topic: String
        var answered: String
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        table.estimatedRowHeight = 50
        table.dataSource = self
        table.delegate = self
        self.table.registerClass(QuestionTableViewCell.self, forCellReuseIdentifier: "cell")
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("cell") as QuestionTableViewCell
        cell.student.text = "random string"
        cell.labDesk?.text = "25/A"
        cell.topic?.text = "string"
        cell.answers?.text = "3"
        return cell
    }
}

8条回答
戒情不戒烟
2楼-- · 2020-05-30 04:03

The most important part is to register the xib containing the custom cell with the table view. Therefore add the following code in viewDidLoad() method.

let nib = UINib.init(nibName: "MyCustomCell", bundle: nil)    
self.tblUsers.register(nib, forCellReuseIdentifier: "MyCustomCell")
查看更多
淡お忘
3楼-- · 2020-05-30 04:10

First, you don't have to register the class if it exists in Interface Builder.

Second, you should dequeueReusableCellWithIdentifier:forIndexPath instead of dequeueReusableCellWithIdentifier.

Third, UITableViewController already has a property called tableView so there is no need to make an IBOutlet to table as UITableViewController already handles this. It also conforms to the UITableViewDataSource and UITableViewDataSource so these are extraneous.

Fourth, don't set the properties for table set them for tableView.

Fifth, cell.labDesk.text = "" is sufficient, no need to make it optional.

If all your IBOutlets are hooked up, Cell Identifiers correctly set, and these revisions are made, it will work.

class QuestionTableViewCell: UITableViewCell {

    @IBOutlet var student: UILabel!
    @IBOutlet var labDesk: UILabel!
    @IBOutlet var topic: UILabel!
    @IBOutlet var answers: UILabel!

}


class QuestionViewController: UITableViewController {

    struct Question {
        var student: String
        var labDesk: String
        var topic: String
        var answered: String
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.estimatedRowHeight = 50
        tableView.dataSource = self
        tableView.delegate = self
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("Cell") as QuestionTableViewCell
        cell.student.text = "random string"
        cell.labDesk.text = "25/A"
        cell.topic.text = "string"
        cell.answers.text = "3"
        return cell
    }
}
查看更多
神经病院院长
4楼-- · 2020-05-30 04:11

Try removing self.table.registerClass(QuestionTableViewCell.self, forCellReuseIdentifier: "cell")

查看更多
家丑人穷心不美
5楼-- · 2020-05-30 04:12

For those who are still trying to figure this out after trying all those possible solutions:

Disconnect/Reconnect the IBOutlets in your Storyboards should do the trick!

查看更多
三岁会撩人
6楼-- · 2020-05-30 04:16

I might be late here, but I just solved a similar problem.

Make sure you've set the Identifier in InterfaceBuilder on your UITableViewCell.

enter image description here

查看更多
孤傲高冷的网名
7楼-- · 2020-05-30 04:17

If you haven't added constraints for the label then they will not be created though the custom cell is created. Make sure you added some constraints.

查看更多
登录 后发表回答