swift - 在函数 swift 3 中发送不同的 Nib

标签 swift uitableview swift3 uicollectionviewcell xib

我有一个 tableView 需要包含两个不同的 View ,第一个的名称是 CustomTableViewCell 第二个是 CustomDeliveryTableViewCell 我想让我的变量取两个单元格,我不明白这个错误。 enter image description here 这是我的功能

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

    var cell: UITableViewCell
    cell = Bundle.main.loadNibNamed("CustomTableViewCell", owner: self, options: nil)?.first as! UITableViewCell

    if (!self.appReady)
    {
        return cell
    }
    let arrayOfCard = self.selectedCard(section: indexPath.section, row: indexPath.row)
    let json:JSON = JSON(arrayOfCard)
    if (json[0]["cards"][indexPath.row]["category"] == "delivery")
    {
        cell = Bundle.main.loadNibNamed("CustomDeliveryTableViewCell", owner: self, options: nil)?.first as! CustomTableViewCell
    }
    cell = fillCell(cell: cell, json: json, index: indexPath.row)
    return cell
}

我的函数 fillCell 就是这样的原型(prototype)

func fillCell(cell: CustomTableViewCell, json:JSON, index:Int) -> CustomTableViewCell

编辑

这里是实际的fillCell函数的代码

    func fillCell(cell: UITableViewCell, json:JSON, index:Int) -> UITableViewCell {
    if (json[0]["cards"][index]["category"] == "train")
    {
        if let type = json[0]["cards"][index]["category"] as JSON?
        {
            cell.labelType.text = type.string
        }
        if let departureStation = json[0]["cards"][index]["train"]["departure"]["station"] as JSON?
        {
            cell.labelDepartureStation.text = departureStation.string
        }
    // Do some code
    }
    else if (json[0]["cards"][index]["category"] == "delivery")
    {
        //Do some code
        return cell
    }
    else{
        //Do some code
        return cell
    }
}

最佳答案

您的初始分配创建了一个类型为 CustomDeliveryTableViewCellcell

if block 中,您试图将 CustomTableViewCell 分配给同一个变量。这仅在 CustomTableViewCellCustomDeliveryTableViewCell

的子类时有效

当您调用 fillCell( 时,它期待 CustomTableViewCell,但 cellCustomDeliveryTableViewCell

如果您声明 var cell: UITableViewCell,那么您可以为其分配任何一种类型。

关于swift - 在函数 swift 3 中发送不同的 Nib ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42724806/

相关文章:

swift - 如何使用 AnyObject.filter 方法?

swift - 在 Core Bluetooth 的中央显示 CBService 和 CBCharacteristic 名称的问题

Swift - 从另一个 UITableViewCell 访问 UITableViewCell

swift - Xcode 8 Swift 3 段错误 : 11

swift - CK订阅错误iOS10

arrays - 将 Int 数组写入文本文件

作为通用参数的 Swift 协议(protocol)

ios - 如何过滤 'updateSearchResultsForSearchController' 中的字典数组以使用 Swift 搜索 UITableView

ios - 需要在 uitableviewCell 中使用 Uicollectionview,并在 swift 中使用 collectionviewcell 的动态高度

ios - UILabel 文本的多种文本颜色在 swift 3.0 中不起作用