How to show 2 customized cells in the UITableView

允我心安 提交于 2019-12-02 10:37:23

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

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