gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 05:34:48 27 4
gpt4 key购买 nike

我有一个 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com