简体   繁体   English

在Swift中自定义单元格之间切换

[英]Switching between custom cells in Swift

I'm trying to switch between two custom cell-classes in swift, but I can't seem to figure out how to return the cell. 我试图快速在两个自定义单元格类之间切换,但是我似乎无法弄清楚如何返回该单元格。

My code looks like this, and the error is in the last line: 我的代码如下所示,错误出现在最后一行:

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

    if istrue{
    var cell: CustomTableCell = self.tv.dequeueReusableCellWithIdentifier("cell") as CustomTableCell

        let data = myList[indexPath.row] as Model

        cell.customLabel.text = data.username
        cell.dateLabel.text = printDate(data.date)
        return cell

    }else{
        var cell: CustomTableCell2 = self.tv.dequeueReusableCellWithIdentifier("cell") as CustomTableCell2

        let data = myList[indexPath.row] as Model

        cell.titleLabel.text = data.username
        cell.dateLabel2.text = printDate(data.date)

     return cell
    }

}return nil

I've also tried to "return cell" in the last line and to delete the other two lines of "return cell" in the if- and else-statements but that didn't work, it just gives me the error saying "cell" is an unresolved identifier. 我也尝试过在最后一行“返回单元格”,并在if和else语句中删除“返回单元格”的其他两行,但这没有用,这只是给我一个错误,说“单元格”是无法解析的标识符。

I've never done this before so I'm not sure if this is the right way of tackling the problem either. 我以前从未做过此事,因此我不确定这是否是解决问题的正确方法。

Any suggestions on how to proceed would be appreciated. 任何建议如何进行将不胜感激。

Define a variable of UITableViewCell type and initialize it in both the if and the else branches, then use it as the return value: 定义UITableViewCell类型的变量,并在if和else分支中对其进行初始化,然后将其用作返回值:

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

    var retCell: UITableViewCell

    if istrue{
        var cell: CustomTableCell = self.tv.dequeueReusableCellWithIdentifier("cell") as CustomTableCell

        let data = myList[indexPath.row] as Model

        cell.customLabel.text = data.username
        cell.dateLabel.text = printDate(data.date)

        retCell = cell

    }else{
        var cell: CustomTableCell2 = self.tv.dequeueReusableCellWithIdentifier("cell") as CustomTableCell2

        let data = myList[indexPath.row] as Model

        cell.titleLabel.text = data.username
        cell.dateLabel2.text = printDate(data.date)

        retCell = cell
    }

    return retCell
}

Note that you cannot return nil because the return type of this method is a non-optional UITableViewCell , so it must always be an instance of (a class derived from) UITableViewCell . 请注意,您不能返回nil因为此方法的返回类型是非可选的UITableViewCell ,因此它必须始终是UITableViewCell (从其派生的类)的实例。

Alternatively, you can just return the cell as you do on each of the if and else branches, but remove the ending return out of the if/else scope - it's not needed. 另外,您也可以像在每个if和else分支上一样返回单元格,但是将结束return从if / else范围内删除-不需要。 Moreover, in your code it is also misplaced because out of the method scope. 此外,在您的代码中,由于方法范围之外,它也被放错了位置。

Personal note: in functions I usually avoid return statements in the middle of the body, preferring a single exit path at the end - that's just a personal preference, so feel free to choose the one that you like more. 个人说明:在函数中,我通常避免在主体中间使用return语句,而是在末尾使用单个退出路径-这只是个人喜好,因此可以随意选择自己喜欢的那个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM