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 2119
面向向阳花
面向向阳花 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:26

    There is two way you can define cell. If your table cell is inside on your ViewControllern then get the cell this way:

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let cell = tableView.dequeueReusableCell(withIdentifier: "TableViewCell", for: indexPath) as! TableViewCell
            // write your code here 
            return cell
    }
    

    But if you define cell outside of your ViewController then call the sell this way:

     func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let cell = Bundle.main.loadNibNamed("TableViewCell", owner: self, options: nil)?.first as! TableViewCell
            // write your code here
            return cell
        }
    

    And as everyone said don't forget to set your cell identifier:

提交回复
热议问题