How to add two custom cell into tableview in storyboard in Swift?

 ̄綄美尐妖づ 提交于 2019-12-06 10:25:10

Check the criteria for showing each specific custom cell, then cast to that cell as needed:

override func tableView(tableView: UITableView?, cellForRowAtIndexPath indexPath: NSIndexPath?) -> UITableViewCell? {

    if (criteria for cell 1) {
        let cell = tableView!.dequeueReusableCellWithIdentifier("cell1", forIndexPath: indexPath) as? Cell1
        return (cell)
    }
    else {
        let cell = tableView!.dequeueReusableCellWithIdentifier("cell2", forIndexPath: indexPath) as? Cell2
        return (cell)
    }
}

Swift 3

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath -> UITableViewCell {

    if (criteria for cell 1) {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell1", for: indexPath) as! Cell1
        return cell
    }
    else {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell2", for: indexPath) as! Cell2
        return cell
    }
}
 override func tableView(tableView: UITableView?,cellForRowAtIndexPath indexPath: NSIndexPath?) -> UITableViewCell? {

if (...) {
    let cell : Mycell1  = tableView!.dequeueReusableCellWithIdentifier("Mycell1", forIndexPath: indexPath) as? Mycell1
    return cell
}
else {
    let cell : Mycell2 = tableView!.dequeueReusableCellWithIdentifier("Mycell2", forIndexPath: indexPath) as? Mycell2
    return cell
   }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!