Custom table view cell: IBOutlet label is nil

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

Consider the following simple view controller:

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


        
相关标签:
6条回答
  • 2020-12-08 18:46

    Please make sure you are not doing any mistake while registering your nib(custom cell) in the viewdidload. Nib name and reuseIdentifiers should not be wrong.

    0 讨论(0)
  • 2020-12-08 18:53

    Works for me without !

    EDIT

        let challenge = self.challenges![indexPath.row]
    
        let title = challenge["name"]
        if title != nil {
            cell.titleLabel.text = title
        }
    
    0 讨论(0)
  • 2020-12-08 18:55

    Register your nib like this:

    let bundle = Bundle(for: type(of: self))
    let nib = UINib(nibName: "PickerCell", bundle: bundle)
    collectionViewPicker.register(nib, forCellWithReuseIdentifier: "cell")
    
    0 讨论(0)
  • 2020-12-08 18:59

    try this

    import UIKit
    
    class ViewController: UIViewController, UITableViewDataSource {
        @IBOutlet weak var tableView: UITableView!
    
        var items = ["One", "Two", "Three"]
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            self.tableView.registerNib(UINib(nibName: "CustomTableViewCell", bundle: nil), forCellReuseIdentifier: "customCell")// CustomTableViewCell.self, forCellReuseIdentifier: "customCell")
            self.tableView.dataSource = self
        }
    
        func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return self.items.count;
        }
    
        func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
            let cell = self.tableView.dequeueReusableCellWithIdentifier("customCell", forIndexPath: indexPath) as CustomTableViewCell
            cell.titleLabel!.text = self.items[indexPath.row]
            return cell
        }
    }
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-08 19:07

    You should use dequeueReusableCellWithIdentifier:forIndexPath to dequeue the cells.

    If you are using storyboard to create the cells, you should not need to register the class for reuse as storyboard does it for you if you set the reuseIdentifier.

    0 讨论(0)
提交回复
热议问题