Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/117.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 子类多单元_Ios_Iphone_Swift_Uitableview - Fatal编程技术网

Ios 子类多单元

Ios 子类多单元,ios,iphone,swift,uitableview,Ios,Iphone,Swift,Uitableview,在我的UITableView中,我有四个单元格。每个单元格都有自己的类。但是我只能返回其中一个单元格。这是我的密码 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let firstCell = tableView.dequeueReusableCellWithIdentifier("FirstCell", forI

在我的
UITableView
中,我有四个单元格。每个单元格都有自己的类。但是我只能
返回其中一个单元格。这是我的密码

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

    let firstCell = tableView.dequeueReusableCellWithIdentifier("FirstCell", forIndexPath: indexPath) as! FirstTableViewCell



    let secondCell = tableView.dequeueReusableCellWithIdentifier("SecondCell", forIndexPath: indexPath) as! SecondTableViewCell

    let thirdCelll = tableView.dequeueReusableCellWithIdentifier("ThirdCell", forIndexPath: indexPath) as! ThirdTableViewCell

    let fourthCell = tableView.dequeueReusableCellWithIdentifier("FourthCell", forIndexPath: indexPath) as! FourthTableViewCell





    return firstCell


}

如何正确地子类并返回它们

此函数被多次调用。
indexPath.row
将用于确定返回哪个单元格。您应该检查这个值,并且只返回请求的单元格

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    var cell

    switch indexPath.row {
    case 0:
        cell = tableView.dequeueReusableCellWithIdentifier("FirstCell", forIndexPath: indexPath) as! FirstTableViewCell

    case 1:
        cell = tableView.dequeueReusableCellWithIdentifier("SecondCell", forIndexPath: indexPath) as! SecondTableViewCell

    case 2:
        cell = tableView.dequeueReusableCellWithIdentifier("ThirdCell", forIndexPath: indexPath) as! ThirdTableViewCell

    case 3:
        cell = tableView.dequeueReusableCellWithIdentifier("FourthCell", forIndexPath: indexPath) as! FourthTableViewCell
    }

    return cell;
}
   func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    if indexPath.row == 0  // or your custom condition
    {    let firstCell = tableView.dequeueReusableCellWithIdentifier("FirstCell", forIndexPath: indexPath) as! FirstTableViewCell
return firstCell
    }

    else if indexPath.row == 1{
        let secondCell = tableView.dequeueReusableCellWithIdentifier("SecondCell", forIndexPath: indexPath) as! SecondTableViewCell
return secondCell
    }
    indexPath.row == 2{
        let thirdCelll = tableView.dequeueReusableCellWithIdentifier("ThirdCell", forIndexPath: indexPath) as! ThirdTableViewCell
return thirdCelll
    }
    else{
        let fourthCell = tableView.dequeueReusableCellWithIdentifier("FourthCell", forIndexPath: indexPath) as! FourthTableViewCell
return fourthCell
    }
 }