创建自定义表格单元并添加所有组件并注册
我在下面创建了这些文件:
PlaceDetailCell.xib
的 id 为<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<podcasts>
<podcast>
<url>http://...</url>
<name>blablabla</name>
<frequency>20 min</frequency>
<category>Drama</category>
<episodes>
<episode>
<name>blablabla</name>
<description>blablabla...</description>
</episode>
<episode>
<name>blablabla</name>
<description>blablabla...</description>
</episode>
</episodes>
</podcast>
<podcast>
<url>http://...</url>
<name>blablabla</name>
<frequency>20 min</frequency>
<category>Drama</category>
<episodes>
<episode>
<name>blablabla</name>
<description>blablabla...</description>
</episode>
<episode>
<name>blablabla</name>
<description>blablabla...</description>
</episode>
</episodes>
</podcast>
</podcasts>
CustomPlaceDetailCell.swift
public class Podcast
{
public string Url { get; set; }
public string EpisodesQuantity { get; set; }
public string Name { get; set; }
public string Frequency { get; set; }
public string Category { get; set; }
public List<Episode> episodeList { get; set; }
}
public class Episode
{
public string Name { get; set; }
public string Desc { get; set; }
public string Link { get; set; }
}
customPlaceDetailCell
这是我的 cellForRowAt
import UIKit
class CustomPlaceDetailCell: UITableViewCell {
@IBOutlet weak var country: UILabel!
@IBOutlet weak var address: UILabel!
@IBOutlet weak var distance: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
当我单击该单元格时崩溃,即使该应用程序已成功构建。
由于未捕获的异常“ NSUnknownKeyException”而终止了应用程序,原因:“ [setValue:forUndefinedKey:]:此类不适用于密钥地址编码的密钥值。” ***首先抛出调用堆栈:
有人可以为我做正确的事情提供一些指导。
答案 0 :(得分:1)
您需要将xib文件自定义类设置为将其强制转换为的类
let cell = tableView.dequeueReusableCell(withIdentifier: "customPlaceDetailCell", for: indexPath)
as! CustomPlaceDetailCell
好像xib名称是PlaceDetailCell
,类为PlaceDetailCell
,您将其强制转换为CustomPlaceDetailCell
如果您注册此
placesTable.register(UINib(nibName: "PlaceDetailCell", bundle: nil), forCellReuseIdentifier: "customPlaceDetailCell")
然后应将xib自定义类设置为CustomPlaceDetailCell
而不是PlaceDetailCell
,因为通常情况下xib名称=它是您创建的类名称,然后想要更改类名称