我有一个 tableView 需要包含两个不同的 View ,第一个的名称是 CustomTableViewCell 第二个是 CustomDeliveryTableViewCell 我想让我的变量取两个单元格,我不明白这个错误。 这是我的功能
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
}
}
最佳答案
您的初始分配创建了一个类型为 CustomDeliveryTableViewCell
的 cell
。
在 if
block 中,您试图将 CustomTableViewCell
分配给同一个变量。这仅在 CustomTableViewCell
是 CustomDeliveryTableViewCell
当您调用 fillCell(
时,它期待 CustomTableViewCell
,但 cell
是 CustomDeliveryTableViewCell
如果您声明 var cell: UITableViewCell
,那么您可以为其分配任何一种类型。
关于swift - 在函数 swift 3 中发送不同的 Nib ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42724806/