NSTableView绑定和tableViewSelectionDidChange

时间:2015-11-25 06:07:46

标签: objective-c cocoa nstableview cocoa-bindings

我有一个带有绑定的NSTableView设置,我已将委托+ dataSource设置为File的所有者。表视图的元素不会触发委托方法。如果我点击元素外部,即表视图背景 - selectionShouldChangeInTableView被调用。

我无法理解为什么不调用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;
}

1 个答案:

答案 0 :(得分: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

并且不要忘记在析构函数方法

中删除观察者
-(void)dealloc 
{
    [super dealloc];
    [myArrayController removeObserver:self];
}