0

I have to show 2 different cells in a table. I have tried it by setting a prototype to a table. but it is still showing Prototype table cells must have reuse identifiers warning.

could someone please guide me to resolve this warning.

Followed this link: UITableview with more than One Custom Cells with Swift

Community
  • 1
  • 1
Dee
  • 1,887
  • 19
  • 47

2 Answers2

1

In storyboard you have to define the Identifier for the cells like the below imageenter image description here

Then in cellForRowAtIndexPath you have to use the specific identifier for specific cell like this

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    if indexPath.row == 0 {
        let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Identifier1")
        //set the data here
        return cell
    }
    else if indexPath.row == 1 {
        let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Identifier2")
        //set the data here
        return cell
    }
}
Rajat
  • 10,977
  • 3
  • 38
  • 55
1

You must set the Reuse Identifier for both prototype cells, and they must be different. Then in your cellForItemAtIndexPath method, you must dequeue the cells using the corresponding Reuse Identifier based on the indexPath given.

Reuse Identifier

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableView {

    switch indexPath.section {
    case 0:
        return tableView.dequeueReusableCellWithIdentifier("CustomCell1", forIndexPath: indexPath)
    case 1:
        return tableView.dequeueReusableCellWithIdentifier("CustomCell2", forIndexPath: indexPath)
    break:
        return UITableViewCell()
    }
}
Callam
  • 11,409
  • 2
  • 34
  • 32