简体   繁体   English

UITableViewController不使用Xib文件加载自定义单元格

[英]UITableViewController not loading custom cells using xib file

I have a UITableViewController and want to populate it with custom cells. 我有一个UITableViewController并想用自定义单元格填充它。 The cell I have created has it's own class and xib file, away from the main storyboard. 我创建的单元格具有它自己的类和xib文件,而远离主故事板。

In the xib file, I have 'VisitCell' as the identifier. 在xib文件中,我有“ VisitCell”作为标识符。

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "VisitCell", for: indexPath) as! VisitCell

    let visit : BookingVisitItem = bookingDayItem.getVisitItems()[indexPath.row]

    cell.locationLabel?.text = visit.getVenueName()
    cell.timeLabel?.text = visit.getVisitTime()
    cell.typeLabel?.text = visit.getVisitType()

    return cell
}

If I run the code like this, it seems to crash. 如果我这样运行代码,似乎会崩溃。 Maybe it can't find the custom cell? 也许找不到自定义单元格?

However, if I add the following to viewDidLoad() 但是,如果我将以下内容添加到viewDidLoad()

tableView.register(VisitCell.self, forCellReuseIdentifier: "VisitCell")

At this stage, it doesn't crash but the table view appears to be empty. 在此阶段,它不会崩溃,但是表视图似乎是空的。

Any pointers? 有指针吗? Thanks! 谢谢!

Register your nib cell in viewDidLoad() -: viewDidLoad()注册您的笔尖单元-:

override func viewDidLoad() {
        super.viewDidLoad()
        yourTableView.registerNib(UINib(nibName: "yourcell", bundle: nil), forCellReuseIdentifier: "CustomCellOne")
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM