Matt Cooper
Matt Cooper

Reputation: 2062

tableViewSelectionDidChange: Not Being Called

I've worked with NSTableView a couple times before, and I've used this method with no issues, but for some reason in my newest program the tableViewSelectionDidChange: delegate method isn't being called when I switch rows. I've created a very simple program to try to get to the source of this, but for some reason it still isn't working. I know I'm probably overlooking something small but I've been staring at this for hours and comparing it to my other code where it works and I can't see anything.

AppDelegate.h:

#import <Cocoa/Cocoa.h>

@interface AppDelegate : NSObject <NSApplicationDelegate, NSTableViewDataSource, NSTableViewDelegate>

//not sure if the NSTableViewDelegate part is needed, as I've used this before without it

@property (assign) IBOutlet NSWindow *window;
@property (weak) IBOutlet NSTableView *tableView;

@end

AppDelegate.m:

#import "AppDelegate.h"

@implementation AppDelegate

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
}
- (void)tableViewSelectionDidChange:(NSNotification *)aNotification{
    NSLog(@"Row changed");
}

- (NSInteger)numberOfRowsInTableView:(NSTableView *)aTableView
{
    return 2;
}

- (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex
{
    return nil;
}

@end

Upvotes: 2

Views: 3780

Answers (4)

everding
everding

Reputation: 41

You need wrap the tableview with a NSViewController such as yourController, set the delegate and dataSource of the tableview to yourController;

self.tableView.delegate = self;
self.tableView.dataSource = self;

Of course, you should implement the delegate methods and the datasource methods in yourController;

Then:

window.contentViewController = yourController;

This works for me.

Upvotes: 0

DarkDust
DarkDust

Reputation: 92384

I also had the problem that the tableViewSelectionDidChange: method wasn't called, but only after I've closed and reopened my dialog. It turned out that this "delegate" method does have a notification observer signature for a reason: Apple simply registers your delegate method with NSNotficationCenter. So if you call [[NSNotificationCenter defaultCenter] removeObserver:self]; like I did in my windowDidHide method, you won't get notified about table selection changes any more.

The solution is instead of being lazy and calling [[NSNotificationCenter defaultCenter] removeObserver:self];, you need to unregister only the notifications that you have explicitly observed before.

Upvotes: 9

El Tomato
El Tomato

Reputation: 6707

Additionally insert the following lines and see what happens. Make sure you have set AppDelegate as source and delegate.

- (BOOL)tableView:(NSTableView *)aTableView shouldSelectRow:(NSInteger)rowIndex {
    return YES;
}

If that doesn't help, I don't know the cause.

Upvotes: 5

El Tomato
El Tomato

Reputation: 6707

You need to set its data source and delegate to AppDelegate by control-clicking on the tableview control and extending the string to AppDelegate's blue icon, if you haven't.

Upvotes: 2

Related Questions