在 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” - 我如何访问类 c1 中定义的方法而不违反 Xcodes 编译检查?
最佳答案
你只需要强制将它转换为c1
:
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell1", for: indexPath) as! c1
cell.celltest()
顺便说一句,c1
不是一个好的类名。
关于swift - 使用 Storyboard中定义的自定义类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56413602/