H.Rabiee
H.Rabiee

Reputation: 4837

NSTableView bindings and tableViewSelectionDidChange

I have an NSTableView setup with bindings and I've setup the delegate+dataSource to File's Owner. The table view's elements are not triggering delegate methods. If I click outside the elements, i.e the table view background - selectionShouldChangeInTableView is called.

I cannot understand why tableViewSelectionDidChange is not called. Seriously why is it so difficult to debug this?

-(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;
}

Upvotes: 1

Views: 550

Answers (1)

AMTourky
AMTourky

Reputation: 1200

If you are interested in the selected row, you may use the delegate method

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

BUT a better approach is to add an observer on selection of the array controller itself, the array controller your table is bound to

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

and listen on changes using:

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

and don't forget to remove the observer at the destructor method

-(void)dealloc 
{
    [super dealloc];
    [myArrayController removeObserver:self];
}

Upvotes: 1

Related Questions