0

Create tableView by storyboard, and in the tableView delegate method:tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell occurs a exception:

*** Assertion failure in -[UITableView dequeueReusableCellWithIdentifier:forIndexPath:],
   /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit_Sim/UIKit-3599.6/UITableView.m:6593

my code is below:

ViewController11

@IBOutlet weak var tableView: UITableView!  // tableView

// tableview delegate
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell_id: String = "cell_id"
    // this line below occurs exception: Thread 1:breakpoint 5.2
    var cell: TableViewCell2 = tableView.dequeueReusableCell(withIdentifier: cell_id, for: indexPath) as!  TableViewCell2

    if cell == nil {

        // 
    }

    var title = dataSource[indexPath.row]

    cell.titlelabel?.text = String(title)


    return cell
}

and my storyboard for ViewController11:

viewController11

and my xib for TableViewCell2:

TableViewcell12

Filburt
  • 17,626
  • 12
  • 64
  • 115
aircraft
  • 25,146
  • 28
  • 91
  • 166

1 Answers1

0

You have to register your UITableViewCell from the xib before you can use it:

Swift 2:

override func viewDidLoad() {
    super.viewDidLoad()
    self.tableView.registerNib(UINib(nibName: "TableViewCell2", bundle: NSBundle.mainBundle()), forCellReuseIdentifier: "cell_id")
}

Swift 3:

override func viewDidLoad() {
    super.viewDidLoad()
    self.tableView.register(UINib(nibName: "TableViewCell2", bundle: Bundle.main), forCellReuseIdentifier: "cell_id")
}
d.felber
  • 5,288
  • 1
  • 21
  • 36