How to show 2 customized cells in the UITableView

后端 未结 2 355
有刺的猬
有刺的猬 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()
        }
    }
    

提交回复
热议问题