默认情况下,nStabLVIEW允许用户通过单击表视图空白区域中的任意位置来清除行选择。然而,这并不总是直观的,有时甚至是不可能的(例如,当表视图实际上没有任何空区域本身)。
那么,如何允许用户通过再次单击该行来取消选中该行呢?在这种情况下,不会调用常规委托方法(如-tableView:shouldSelectRow:
),因此您无法通过这种方式捕获对已选定行的单击。
最佳答案
您想定义自己的nstableview子类并设置-mouseDown:
如下:
- (void)mouseDown:(NSEvent *)theEvent {
NSPoint globalLocation = [theEvent locationInWindow];
NSPoint localLocation = [self convertPoint:globalLocation fromView:nil];
NSInteger clickedRow = [self rowAtPoint:localLocation];
BOOL wasPreselected = (self.selectedRow == clickedRow);
[super mouseDown:theEvent];
if (wasPreselected)
[self deselectRow:self.selectedRow];
}