How to show 2 custom cells in a UITableView

I need to show 2 different cells in a table. I tried this by installing a prototype table. but it still shows a Prototype table cells must have reuse identifiers warning.

Can someone please help me clear this warning.

Follow this link: UITableview with more than one user cell with Swift

0
ios uitableview swift custom-cell
Sep 15 '16 at 16:11
source share
2 answers

In the storyboard, you must define an ID for cells such as the image below enter image description here

Then in cellForRowAtIndexPath you need to use a specific identifier for a 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 } } 
+1
Sep 15 '16 at 16:38
source share

You must install a Reuse Identifier for both prototypes, and they must be different. Then, in your cellForItemAtIndexPath method cellForItemAtIndexPath you must deactivate the cells using the appropriate Reuse Identifier based on the specified indexPath.

Reuse id

 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() } } 
+1
Sep 15 '16 at 16:41
source share



All Articles