I am attempting to setup a custom UITableViewCell in Swift. I have created the XIB file and swift file as well. However, when I run the app the cells are not populated with my custom cell, but rather a completely blank cell.
I have registered my cell with:
self.myTableView.registerClass(customCell.self, forCellReuseIdentifier: "cell")
I have also setup the following:
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 3
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = myTableView.dequeueReusableCellWithIdentifier("cell") as! customCell
cell.descriptionLabel.text = "test"
return cell
}
In addition, I have setup a link to a UILabel in the custom cell. When I try to update it the app crashes. The line is cell.descriptionLabel.text = "test"
as shown above.
Instead of register the UITableViewCell
class, you must register the nib, for example:
self.tableView.register(UINib(nibName: "CustomTableViewCell", bundle: nil), forCellReuseIdentifier: "cell")
The nibName
is the name of the XIB file but without the .xib
extension.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.