gpt4 book ai didi

objective-c - 使用鼠标切换 TableView 单元格的选定状态

转载 作者:搜寻专家 更新时间:2023-10-30 19:43:58 24 4
gpt4 key购买 nike

默认情况下,NSTableView 允许用户通过单击 TableView 空白区域的任意位置来清除行选择。然而,这并不总是直观的,有时甚至是不可能的(例如,当 TableView 本身实际上没有任何空白区域时)。

那么如何让用户通过简单地再次点击它来取消选择呢?在这种情况下,不会调用常规委托(delegate)方法(如 -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];

}

关于objective-c - 使用鼠标切换 TableView 单元格的选定状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19393242/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com