I made a view in a xib file which is loaded in the main ViewController called "ExperienceScreen". Adding this xib view to the ExperienceScreen works perfectly. The problem is that I would like to add this xib view in a UITableViewCel. I am using the following code to do that :
let experiences = service.getExperiences()
// 3. Loop through the array of experiences
for element in experiences {
if let customView = Bundle.main.loadNibNamed("ExperienceDetail", owner: self, options: nil)?.first as? ExperienceDetail
{
customView.lblTitle.text = element.title
customView.lblCompany.text = element.company
let cell = tableView.dequeueReusableCell(withIdentifier: "cell")! as UITableViewCell
cell.addSubview(customView)
cell.bringSubview(toFront: customView)
}
}
tableView.reloadData()
When launching, the subview is not shown in the UITableViewCell. The customView View is filled correctly with the xib View. I checked this using a breakpoint.
Someone knows what I'am doing wrong?
Many thanks for helping !!
If you want to display your xib file as UITableViewCell, then following scenario works 1. make sure your xib class is sub class of UITableViewCell. 2. register your xib
//class of xib file
class TableCell: UITableViewCell {
static let identifier = "TableCell"
static let nib = UINib(nibName: "TableCell", bundle: nil)
}
// In view controller
func setupTableView() {
tableView.dataSource = self
tableView.delefate = self
tableView.register(TableCell.nib, forCellReuseIdentifier:
TableCell.identifier)
}
call setupTableView() in viewDidLoad()