unable to dequeue a cell with identifier Cell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard

前端 未结 22 2117
面向向阳花
面向向阳花 2020-12-12 17:33

I am fairly new to coding in general and really new to Xcode (Swift). I understand that I need to register a nib or a class but I don\'t understand \'where or how?\'.

<
相关标签:
22条回答
  • 2020-12-12 18:28

    In the “Subclass of” field, select UITableViewController.

    The class title changes to xxxxTableViewController. Leave that as is.

    Make sure the “Also create XIB file” option is selected.

    0 讨论(0)
  • 2020-12-12 18:29

    My problem was I was registering table view cell inside dispatch queue asynchronously. If you have registered table view source and delegate reference in storyboard then dispatch queue would delay the registration of cell as name suggests it will happen asynchronously and your table view is looking for the cells.

    DispatchQueue.main.async {
        self.tableView.register(CampaignTableViewCell.self, forCellReuseIdentifier: CampaignTableViewCell.identifier())
        self.tableView.reloadData()
    }
    

    Either you shouldn't use dispatch queue for registration OR do this:

    DispatchQueue.main.async {
        self.tableView.dataSource = self
        self.tableView.delegate = self
        self.tableView.register(CampaignTableViewCell.self, forCellReuseIdentifier: CampaignTableViewCell.identifier())
        self.tableView.reloadData()
    }
    
    0 讨论(0)
  • 2020-12-12 18:30

    Stupid mistake:

    make sure you add register(TableViewCell.self, forCellReuseIdentifier: "Cell") instead of register(TableViewCell.self, forHeaderFooterViewReuseIdentifier: "Cell")

    0 讨论(0)
  • 2020-12-12 18:35

    Swift 5

    you need to use UINib method to register cell in viewDidLoad

    override func viewDidLoad() 
    {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    
        //register table view cell        
        tableView.register(UINib.init(nibName: "CustomTableViewCell", bundle: nil), forCellReuseIdentifier: "CustomTableViewCell")
    }
    
    0 讨论(0)
提交回复
热议问题