ios - 在 Interface Builder 中使用 Swift 内部类

标签 ios xcode swift

我有一个内部自定义 UITableViewCell 类(类中的类),我想在 Interface Builder 中使用它。但是,它在那里无法识别它。

import UIKit

class ContactListViewController: UIViewController {
    class Cell: UITableViewCell {
        @IBOutlet weak var name: UILabel!
        @IBOutlet weak var phone: UILabel!
    }
    
    @IBOutlet weak var tableView: UITableView!
}

screenshot

上图配置无效。有办法吗?

最佳答案

您需要使用 @objc 为您的嵌套类指定 objective-c 名称,然后在 Interface Builder 中使用它:

import UIKit

class ContactListViewController: UIViewController {
    @objc(CLCell) class Cell: UITableViewCell {
        @IBOutlet weak var name: UILabel!
        @IBOutlet weak var phone: UILabel!
    }

    @IBOutlet weak var tableView: UITableView!
}

enter image description here

关于ios - 在 Interface Builder 中使用 Swift 内部类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35595895/

相关文章:

java - 在 Swift 中使用反向传播的简单神经网络

ios - SKSpriteNode 出现在 iOS 8 中,而不是出现在 iOS 7 中

ios - NSData荒谬的长度错误

ios - xamarin ios 像 Android 一样裁剪图像

iphone - 如何像 iphone 照片库在 iOS 上那样在网格中显示图像

ios - Xcode 机器人 : cloning multiple repositories before build

objective-c - 已配置默认应用

ios - 当应用程序处于后台或运行状态时而不是应用程序终止时处理推送通知

ios - AVPlayerItemDidPlayToEndTimeNotification 未触发

swift - 如何计算数学类型并在快速输入时给出结果