我正在为我的Swift应用程序创建一个设置页面,并且(下面的示例)我已经将aUITableViewCell
子类化,并且在init()
处理程序中,我将accessoryView
设置为aUISwitch()
。
我在子类上还有一个自定义的@IBInspectable
属性,它也被标记为@IBDesignable
:
@IBDesignable class TableViewSwitchCell: UITableViewCell {
@IBInspectable var settingsKey: String?
func build()
{
accessoryView = UISwitch()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
build()
}
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
build()
}
// code omitted that uses the settingsKey with NSUserDefaults
}
我如何安排
UISwitch
在InterfaceBuilder中也可见,以便在仅从UITableViewCell
进行子类化的情况下将附加目标附加到它理想情况下,我不想创建一个具有自己布局的全新nib,因为我仍然希望保留标准
UITableViewCell
的大多数其他特性。 最佳答案
在单元内部的接口生成器中删除UISwitch
。然后把它和你的细胞连接起来。在标准基本单元上,控件往往锁定在IB中的单元中间。但是在运行时,它将被正确定位。