我在一个名为“ExperienceScreen”的主ViewController中加载的xib文件中创建了一个视图。将这个xib视图添加到ExperienceScreen可以很好地工作。问题是我想在UITableViewCel中添加这个xib视图。我使用下面的代码来做到这一点:在一个UITableView单元中显示一个xib视图
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()
启动时,将子视图没有在UITableViewCell中所示。 customView视图用xib View正确填充。我用断点检查了这个。
有人知道我做错了什么吗?
非常感谢您的帮助!
感谢您的回复
调用setupTableView()!不幸的是,它仍然没有显示。您是否在我的代码中看到此问题: https://www.dropbox.com/s/zr2eknaeovks381/Experiences.swift?dl=0 https://www.dropbox.com/s/op6kg8a4eh2u0i0/ExperienceDetail.swift ?dl = 0 –
In cellForRowAt change let cell = tableView.dequeueReusableCell(withIdentifier:“cell”)!作为UITableViewCell 让cell = tableView.dequeueReusableCell(withIdentifier:ExperienceDetail.identifier)as! ExperienceDetail 和根据您的需要填充表CellCell –
非常感谢! –