Custom table view cell: IBOutlet label is nil

前端 未结 6 1582
走了就别回头了
走了就别回头了 2020-12-08 18:38

Consider the following simple view controller:

class ViewController: UIViewController, UITableViewDataSource {
    @IBOutlet weak var tableView: UITableView!         


        
6条回答
  •  难免孤独
    2020-12-08 19:01

    First off, you're using a nib file to load your custom cell into the table. That's probably going to be more of a headache than it's worth if you're new to Swift/Cocoa. I would move everything over to storyboard for the time being. Instead of using a nib file click, go to Storyboard, click on your UITableView and make sure the TableView's content setting is Dyanamic Prototypes:

    enter image description here

    Next, click on the prototype cell (the only one in the table view) and set the class to CustomTableViewCell and set its reuse identifier to customCell:

    enter image description here enter image description here

    Next, add a label to your prototype cell and link it to the IBOutlet in your CustomTableViewCell class. You don't need to register your customCell so long as you've set the reuse identifier in storyboard. Delete this line:

    self.tableView.registerClass(CustomTableViewCell.self, forCellReuseIdentifier: "customCell")
    

    and it should run.

提交回复
热议问题