【发布时间】:2013-10-27 22:07:35
【问题描述】:
在我基于视图的 NSTableView 中,我不想取消选择选定的单元格,当单击 NSTableView 的空白部分时,如何遵守此默认行为?
【问题讨论】:
标签: cocoa nstableview
在我基于视图的 NSTableView 中,我不想取消选择选定的单元格,当单击 NSTableView 的空白部分时,如何遵守此默认行为?
【问题讨论】:
标签: cocoa nstableview
改进版来自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];
}
}
我们只是忽略事件,当我们没有击中一行时......
【讨论】:
通过子类化实现 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];
}
}
【讨论】:
如果有人需要,这是 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)
}
}
【讨论】: