
我继承了一个名为MyTextFieldCell的NSTextFieldCell。我在其中添加了一个NSButtonCell。
有人知道如何在MytextFieldCell中让NSButttonCell点击吗?
代码:
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)
}
}发布于 2016-07-09 10:33:17
当你在一个单元格中创建一个按钮时,我建议你这样做
selectionStyle = .None
在cell类的awakeFromNib()内部开始,以防止单元格在用户尝试单击按钮时认为您正在单击它,如下所示……
override func awakeFromNib() {
super.awakeFromNib()
selectionStyle = .None
}在button类中为您的按钮创建插座。处理对按钮的操作的最简单方法是在ViewController类而不是单元格中创建它。可以在单元格中创建@IBAction并使用委托与viewController通信,但最简单的方法是在ViewController本身中创建@IBAction。虽然@IBOutlet必须在Cell的类中,但在那里创建它是完全大声的。然后,如果需要以某种方式更改按钮,请在表视图的cellForRowAtIndexPath中更改它,并调用
tableView.reloadRowsAtIndexPaths(NSIndexPath(forRow: theCellRow, inSection: theCellSection), withRowAnimation: UITableViewRowAnimation.None)
在您想要看到更改发生并且更改之前已经发生的时候。我希望这能有所帮助!;)
https://stackoverflow.com/questions/38277193
复制相似问题