在 tableview cellForRowAtIndexPath 中切换

switch inside tableview cellForRowAtIndexPath

我想在 tabview cellForRowAtIndexPath 中使用 switch 函数来更改 table

的内容

场景:标志变量应该 select 是什么情况并且 return 它是内容。

错误:我必须在开关功能之外使用 return 单元格,如果我使用它仍然给我错误。

switch (flag){
    case 0 :

        let cell:mineCell = tableView.dequeueReusableCellWithIdentifier("mineCell")as! mineCell
        // User Name on cells
        cell.usernameLabel.text = creator[indexPath.row]

        //function

            else {
                print(error)
            }
        }

        return cell
        break

    case 1 :
        let cell:followingCell = tableView.dequeueReusableCellWithIdentifier("followingCell")as! followingCell
        // User Name on cells
        cell.followingNameLabel.text = creator[indexPath.row]

    //ffuntion

            else {
                print(error)
            }
        }

        return cell

        break
    case 2 :

        //funtion 
        break
    default:
        break

    }



 //*HERE i have to use return cell but if i use it will give me error in somehow * 

}

您的 cellForRowAtIndexPath 必须 return 一个 UITableViewCell 对象或它的子类在每个可能的场景中。因此,在默认情况下,您必须 return a UITableViewCell() 才能消除错误。

在您的 cellForRow 方法中试试这个

switch (flag){

 case 0 :

        let cell:mineCell = tableView.dequeueReusableCellWithIdentifier("mineCell")as! mineCell
        // User Name on cells
        cell.usernameLabel.text = creator[indexPath.row]

        //function

            else {
                print(error)
            }
        }

        return cell
        break

    case 1 :
        let cell:followingCell = tableView.dequeueReusableCellWithIdentifier("followingCell")as! followingCell
        // User Name on cells
        cell.followingNameLabel.text = creator[indexPath.row]

    //ffuntion

            else {
                print(error)
            }
        }

        return cell

        break
    }     
}

希望对您有所帮助。

首先你需要在全局中声明UITableViewCell对象。

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell:UITableViewCell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell

        switch (flag) {
            case 0 :
            {
                cell = tableView.dequeueReusableCellWithIdentifier("mineCell")as! mineCell
                // User Name on cells
                cell.usernameLabel.text = creator[indexPath.row]
                //function
                if()
                {
                }else {
                    print(error)
                }
               return cell
            }

                break
            case 1 :
            {

            cell = tableView.dequeueReusableCellWithIdentifier("followingCell")as! followingCell
            // User Name on cells
            cell.followingNameLabel.text = creator[indexPath.row]

            //ffuntion
                if()
                {
                }
            else {
                print(error)
            }
           return cell
        }
        break
            default:
                break
        }


    return cell
    }