Reputation: 427
I can't seem to figure out why the cells are all blank in my table with custom cells. I've been searching and trying suggestions for a while now but can't figure it out. At this point I feel like I'm missing something obvious
This is my .swift
import UIKit
struct Contact {
var name:String
var title:String
var phone:String
}
class ContactTableViewCell: UITableViewCell {
@IBOutlet weak var contactName: UILabel!
@IBOutlet weak var contactTitle: UILabel!
@IBOutlet weak var contactPhone: UILabel!
}
class Contacts:UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var contactTable: UITableView!
var contactData = [
Contact(name: "Main Office", title: "Title", phone: "Phone Here"),
Contact(name: "Joe Bloggs", title: "Title Here", phone: "Phone Number"),
Contact(name: "John Smith", title: "Another Title", phone: "Another Phone")
]
override func viewDidLoad() {
super.viewDidLoad()
contactTable.register(ContactTableViewCell.self, forCellReuseIdentifier: "cellId")
contactTable.delegate = self
contactTable.dataSource = self
}
func tableView(_ contactTable: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.contactData.count
}
func numberOfSectionsInTableView(contactTable: UITableView) -> Int {
return 1
}
func tableView(_ contactTable: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let contactCell = contactTable.dequeueReusableCell(withIdentifier: "cellId", for: indexPath) as! ContactTableViewCell
let contact = contactData[indexPath.row]
contactCell.contactName?.text = contact.name
contactCell.contactTitle?.text = contact.title
contactCell.contactPhone?.text = contact.phone
return contactCell
}
}
My IBOutlets are definitely linked, and the cell is linked to the ContactTableViewClass
This is how my cell looks
Any help would be greatly appreciated, thank you
Upvotes: 0
Views: 30
Reputation: 100503
Comment this line
contactTable.register(ContactTableViewCell.self, forCellReuseIdentifier: "cellId")
And set the identifier cellId
to the cell in IB , When you create a prototype cell , don't register it , as the register occurs automatically
In your case all outlets are nil
// try contactCell.contactName.text and it' ll crash
contactCell.contactName?.text = contact.name
contactCell.contactTitle?.text = contact.title
contactCell.contactPhone?.text = contact.phone
As you override IB registration with a class whose layout is created in IB as ContactTableViewCell.self
is when you completely create the class with it's layout programmatically
Upvotes: 1