子类多个单元格

时间:2015-07-31 12:50:13

标签: ios iphone swift uitableview

在我的UITableView我有四个细胞。每个单元格都有自己的类。但我只能return其中一个细胞。这是我的代码

 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


}

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

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

多次调用此函数。 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;
}