How to show 2 customized cells in the UITableView

时光总嘲笑我的痴心妄想 提交于 2019-12-02 20:14:01

问题


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


回答1:


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
    }
}



回答2:


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()
    }
}


来源:https://stackoverflow.com/questions/39515763/how-to-show-2-customized-cells-in-the-uitableview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!