在我的基于NSTableView的视图中,我不想取消选择所选单元格,当点击NSTableView的空白部分时,如何遵守此默认行为?
答案 0 :(得分:3)
改进版(这符合选择/取消选择)
子类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];
}
}
我们只是忽略了这个事件,当我们没有碰到一排......
答案 1 :(得分: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];
}
}
答案 2 :(得分: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)
}
}