How to show 2 customized cells in the UITableView

后端 未结 2 354
有刺的猬
有刺的猬 2021-01-26 06:25

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

相关标签:
2条回答
  • 2021-01-26 07:24

    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.

    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()
        }
    }
    
    0 讨论(0)
  • 2021-01-26 07:30

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

    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
        }
    }
    
    0 讨论(0)
提交回复
热议问题