开发者

Deselecting NSTableView when clicked somewhere outside the table

开发者 https://www.devze.com 2023-01-11 16:50 出处:网络
I have a NSTableView place开发者_JS百科d on top of NSView. I want to deselect the NSTableView when mouse pointer is clicked on NSView. How to achieve this?I know this is old, but one option is to subc

I have a NSTableView place开发者_JS百科d on top of NSView. I want to deselect the NSTableView when mouse pointer is clicked on NSView. How to achieve this?


I know this is old, but one option is to subclass NSTableView and override its mouseDown:

- (void)mouseDown:(NSEvent *)event {

    [super mouseDown:event];

    NSPoint point = [self convertPoint:[event locationInWindow] fromView:nil];
    NSInteger row = [self rowAtPoint:point];

    if (row == -1) { // We didn't click any row

        [self deselectAll:nil];
    }
}

Swift 3 version:

override open func mouseDown(with event: NSEvent) {
    super.mouseDown(with: event)

    let point    = convert(event.locationInWindow, from: nil)
    let rowIndex = row(at: point)

    if rowIndex < 0 { // We didn't click any row
        deselectAll(nil)
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号