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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With