Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Cocoa 在外部单击时,不要在基于视图的NStableview中取消选择所选行_Cocoa_Nstableview - Fatal编程技术网

Cocoa 在外部单击时,不要在基于视图的NStableview中取消选择所选行

Cocoa 在外部单击时,不要在基于视图的NStableview中取消选择所选行,cocoa,nstableview,Cocoa,Nstableview,在基于视图的NSTableView中,当单击NSTableView的空白部分时,我不想取消选择所选单元格,如何遵守此默认行为??通过子类化来实现NSTableView的鼠标按下事件。在其内部检查单击的点是一行还是一个空白区域。如果是空白区域,请再次选择表视图中以前选定的行 - (void)mouseDown:(NSEvent *)theEvent { NSPoint globalLocation = [theEvent locationInWindow]; NSPoint localLocati

在基于视图的NSTableView中,当单击NSTableView的空白部分时,我不想取消选择所选单元格,如何遵守此默认行为?

通过子类化来实现NSTableView的鼠标按下事件。在其内部检查单击的点是一行还是一个空白区域。如果是空白区域,请再次选择表视图中以前选定的行

- (void)mouseDown:(NSEvent *)theEvent
{
NSPoint globalLocation = [theEvent locationInWindow];
NSPoint localLocation = [self convertPoint:globalLocation fromView:nil];
NSInteger clickedRow = [self rowAtPoint:localLocation];

NSIndexSet* selectedRows = [self selectedRowIndexes];
NSLog(@"%ld",clickedRow);
[super mouseDown:theEvent];

if(clickedRow == -1)
{
    [self selectRowIndexes:selectedRows byExtendingSelection:NO];
}

}

通过对NSTableView进行子类化,实现它的鼠标按下事件。在其内部检查单击的点是一行还是一个空白区域。如果是空白区域,请再次选择表视图中以前选定的行

- (void)mouseDown:(NSEvent *)theEvent
{
NSPoint globalLocation = [theEvent locationInWindow];
NSPoint localLocation = [self convertPoint:globalLocation fromView:nil];
NSInteger clickedRow = [self rowAtPoint:localLocation];

NSIndexSet* selectedRows = [self selectedRowIndexes];
NSLog(@"%ld",clickedRow);
[super mouseDown:theEvent];

if(clickedRow == -1)
{
    [self selectRowIndexes:selectedRows byExtendingSelection:NO];
}

}

Neha答案中的改进版(遵循选择/取消选择)

子类NSTableView和implement:

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

    NSPoint globalLocation = [theEvent locationInWindow];
    NSPoint localLocation = [self convertPoint:globalLocation fromView:nil];
    NSInteger clickedRow = [self rowAtPoint:localLocation];

    if(clickedRow != -1) {
        [super mouseDown:theEvent];
    }
}

我们只是忽略事件,当我们没有点击一行时…

Neha答案的改进版(这服从选择/取消选择)

子类NSTableView和implement:

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

    NSPoint globalLocation = [theEvent locationInWindow];
    NSPoint localLocation = [self convertPoint:globalLocation fromView:nil];
    NSInteger clickedRow = [self rowAtPoint:localLocation];

    if(clickedRow != -1) {
        [super mouseDown:theEvent];
    }
}

我们只是忽略事件,当我们没有点击一行时…

如果有人需要,这是Swift4版本:

override func mouseDown(with event: NSEvent) {
    let globalLocation = event.locationInWindow
    let localLocation = convert(globalLocation, from: nil)
    let clickedRow = row(at: localLocation)

    if clickedRow != -1 {
        super.mouseDown(with: event)
    }
}

如果有人需要,可以使用Swift4版本:

override func mouseDown(with event: NSEvent) {
    let globalLocation = event.locationInWindow
    let localLocation = convert(globalLocation, from: nil)
    let clickedRow = row(at: localLocation)

    if clickedRow != -1 {
        super.mouseDown(with: event)
    }
}

太好了,进步1分:)太好了,进步1分:)