2010-08-17 83 views
1

我有一個NSTableView放置在NSView的頂部。我想在NSView上單擊鼠標指針時取消選擇NSTableView。如何實現這一目標?取消選擇NSTableView時,點擊表格外的某處

+0

你是什麼意思「之上」?您通常不應該在視圖層次結構中將其中一個視圖置於其同一個視圖之上;你應該只在其他視圖中放置視圖*。換句話說,兩個兄弟姐妹的觀點不應該重疊;你應該讓其中一個爲子視圖。 – 2010-08-17 10:07:15

+0

那是正確的。一個是另一個的子視圖。但是,如何實現刪除表的功能? – Subrat 2010-08-18 04:07:37

+0

你是什麼意思的「取消選擇表視圖」?撤消其第一響應者狀態,或取消選擇其中的任何選定行? – 2010-08-18 04:51:12

回答

6

我知道這是舊的,但一個選項是繼承NSTableView的並覆蓋其鼠標按下:

- (void)mouseDown:(NSEvent *)event { 

    [super mouseDown:event]; 

    NSPoint point = [self convertPoint:[event locationInWindow] fromView:nil]; 
    NSInteger row = [self rowAtPoint:point]; 

    if (row == -1) { // We didn't click any row 

     [self deselectAll:nil]; 
    } 
} 

斯威夫特3版本:

override open func mouseDown(with event: NSEvent) { 
    super.mouseDown(with: event) 

    let point = convert(event.locationInWindow, from: nil) 
    let rowIndex = row(at: point) 

    if rowIndex < 0 { // We didn't click any row 
     deselectAll(nil) 
    } 
} 
+0

我認爲調用超類來處理mouseDown:事件是不可取的,因爲你似乎完全處理了事件。 – Fnord23 2015-03-10 14:55:02

+0

@ Fnord23不,超級仍需要處理'mouseDown'上的行選擇,這個只處理取消選擇。 – hnh 2016-10-18 19:00:10

相關問題