可在Swift中的NSTextFieldCell中单击NSButtonCell

时间:2016-07-09 00:50:29

标签: swift cocoa

enter image description here
我将一个名为MyTextFieldCell的NSTextFieldCell子类化。 我在其中添加了一个NSButtonCell。
有谁知道,如何让NSButttonCell可以在MytextFieldCell中点击?

代码:

class MyTextFieldCell: NSTextFieldCell {

    var checkButtonCell: NSButtonCell

    required init?(coder aDecoder: NSCoder) {

        let cell = NSButtonCell()
        cell.bezelStyle = .ThickerSquareBezelStyle
        cell.setButtonType(.SwitchButton)
        cell.title = nil
        self.checkButtonCell = cell

        super.init(coder: aDecoder)
    }

    override func drawWithFrame(cellFrame: NSRect, inView controlView: NSView) {

        var checkRect = NSRect()
        var textRect = NSRect()
        NSDivideRect(cellFrame, &checkRect, &textRect, 21.0, NSRectEdge.MaxX)

        super.drawWithFrame(cellFrame, inView: controlView)
        self.checkButtonCell.drawWithFrame(checkRect, inView: controlView)
    }
}

1 个答案:

答案 0 :(得分:0)

当您在单元格中创建按钮时,我建议执行

selectionStyle = .None

在Cell类的awakeFromNib()内开始,以防止当用户尝试单击按钮时单元格会单击它...

override func awakeFromNib() {
    super.awakeFromNib()
    selectionStyle = .None
}

在Button类中为按钮创建插座。处理按钮操作的最简单方法是在ViewController类而不是单元格中创建它。可以在单元格内创建它并使用委托与viewController进行通信,但最简单的方法是在ViewController本身中创建@IBAction。虽然@IBOutlet必须在Cell的类中,但完全大声创建它。然后,如果您需要以某种方式更改按钮,请在表格视图cellForRowAtIndexPath内更改并调用

tableView.reloadRowsAtIndexPaths(NSIndexPath(forRow: theCellRow, inSection: theCellSection), withRowAnimation: UITableViewRowAnimation.None)

您希望看到更改发生并且之前已进行了更改。我希望这有帮助! ;)