iOS UITableViewCell with custom accessoryView in InterfaceBuilder

北慕城南 提交于 2020-01-07 03:06:22

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!