swift - 使用 Storyboard中定义的自定义类

标签 swift xcode uitableview uikit

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

enter image description here

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/

相关文章:

ios - 如何使用多个 TableView 单元格执行 segue

iphone - 当单元格具有 UIImageView subview 时,UITableView 会出现不稳定的滚动

ios - `audioPlayerDidFinishPlaying` 未被调用

ios - 在自定义 UITableViewCell 中包含的 UITableView 中显示相同的内容

objective-c - Finder Sync Extensions 是否需要配置文件?

iphone - 核心数据本地化

iOS 11 屏幕截图标记

ios - 使用 Share 扩展共享类和数据库

iphone - 在 Cocos 3d 中动态加载 3d 对象

iphone - 不允许将单元格移动到另一个部分