【问题标题】:Dont deselect a selected row in view based NStableview when clicked outside在外部单击时,不要取消选择基于视图的 NStableview 中的选定行
【发布时间】:2013-10-27 22:07:35
【问题描述】:

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

【问题讨论】:

    标签: cocoa nstableview


    【解决方案1】:

    改进版来自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];
        }
    }
    

    我们只是忽略事件,当我们没有击中一行时......

    【讨论】:

    • 太棒了,改进+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];
    }
    
    }
    

    【讨论】:

      【解决方案3】:

      如果有人需要,这是 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)
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-02-07
        • 1970-01-01
        • 2011-12-18
        • 2012-03-16
        • 1970-01-01
        相关资源
        最近更新 更多