Custom table view cell: IBOutlet label is nil
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
}
}
Register your nib like this:
let bundle = Bundle(for: type(of: self))
let nib = UINib(nibName: "PickerCell", bundle: bundle)
collectionViewPicker.register(nib, forCellWithReuseIdentifier: "cell")
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
:
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
:
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.