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

问题描述 投票:1回答:3

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

cocoa nstableview
3个回答
3
投票

来自Neha答案的改进版本(这符合选择/取消选择)

子类NSTableView并实现:

- (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];
    }
}

我们只是忽略了这个事件,当我们没有连续...


2
投票

通过子类化实现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];
}

}

0
投票

这是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)
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.