在Xcode中,我可以在标识符检查器中定义一个自定义类,但是如何使用它们呢?下面是示例:

class c1 : UITableViewCell {
func celltest() {
let i = 99;
}
}
class NicePlaceTableViewController: UITableViewController {
.
.
.
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell1", for: indexPath)
**cell.celltest()** .. has no member celltest
let place = places[indexPath.row]
cell.textLabel?.text = place.name
cell.detailTextLabel?.text = "\(place.timestamp)"
return cell
}如果reuseIdentifier是已知的,但不知道它的自定义类--在本例中是" c1“--我如何才能在不违反Xcode编译检查的情况下访问类c1中定义的方法?
发布于 2019-06-02 18:06:52
Xcode Interface Builder是一个可视化创建UI扫描的IDE。
您对表视图中的单元格的配置将使用在表视图中注册的c1单元格实例化一个NicePlaceTableViewController。
如果没有使用Interface Builder工具的Xcode,则必须自己编程创建它,如下所示:
class NicePlaceTableViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
tableView.register(c1.self, forCellReuseIdentifier: "Cell1")
}
}这段代码为给定的标识符注册一个类类型。要访问单元,您必须使用tableView.dequeueReusableCell方法,该方法将作用于内部池来创建或重用单元。
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell1", for: indexPath)
}出列的信元是UITableViewCell类型。如果你想将它转换为c1类型,那么你必须使用!操作符。
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell1", for: indexPath) as! c1https://stackoverflow.com/questions/56413602
复制相似问题