问题
I'm creating a settings page for my Swift app and (following examples here on SO) I've subclassed a UITableViewCell
and in the init()
handlers I've set the accessoryView
to be a UISwitch()
.
I also have a custom @IBInspectable
property on the subclass which is also marked @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
}
How can I arrange for the UISwitch
to also be visible in InterfaceBuilder so that I can attach additional targets to it whilst still just subclassing from UITableViewCell
Ideally I don't want to create a whole new nib with its own layout, since I still want to retain most of the other features of a standard UITableViewCell
.
回答1:
Drop UISwitch
in Interface Builder inside of cell. Then wire it with up accessoryView
of your cell. On standard basic cells the control tends to be locked in the middle of cell in IB. However in runtime it will be properly positioned.
来源:https://stackoverflow.com/questions/35088176/ios-uitableviewcell-with-custom-accessoryview-in-interfacebuilder