2015-11-25 120 views
1

我有一個綁定NSTableView設置,我已經設置委託+數據源文件的所有者。表視圖的元素不會觸發委託方法。如果我點擊元素外部,即表視圖背景 - selectionShouldChangeInTableView被調用。NSTableView綁定和tableViewSelectionDidChange

我不明白爲什麼tableViewSelectionDidChange沒有被調用。嚴重的是爲什麼調試這麼困難?

-(void)tableViewSelectionDidChange:(NSNotification *)notification { 
    NSLog(@"%s", __PRETTY_FUNCTION__); 
    NSTableView *tableView = [notification object]; 
    [[NSNotificationCenter defaultCenter] postNotificationName:TABLE_SELECTION object:tableView]; 

} 

- (BOOL)selectionShouldChangeInTableView:(NSTableView *)tableView { 
    NSLog(@"Hello there"); 
    return YES; 
} 
+0

刪除觀察者顯示你的方法的實現,或者至少,方法頭。 –

+0

更新了問題。 – Rabiees

+1

'selectionShouldChangeInTableView'總是返回YES嗎? – Willeke

回答

1

如果你有興趣的所選行中,你可以使用委託方法

- (BOOL)tableView:(NSTableView *)aTableView shouldSelectRow:(NSInteger)rowIndex 

一個更好的方法是在陣列控制器本身的選擇增加一個觀察者,數組控制器在表綁定到一個

[myArrayController addObserver:self forKeyPath:@"selection" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil]; 

,並聽取關於使用變化:

- (void)observeValueForKeyPath:(NSString *)keyPath 
         ofObject:(id)object 
         change:(NSDictionary<NSString *, 
             id> *)change 
         context:(void *)context 

,不要忘記在析構函數方法

相關問題