views:

172

answers:

4

Hi! I see there are a lot of questions about this, but nothing helped me to get this work. I have a nib with a NSTableView with three columns (with the right identifiers set) and a class named ShortcutsTableController. In the nib I have a NSObject with class value ShortcutsTableController. I also connected the NSTableView to my controller as I usually do.

This is header ShortcutsTableController.h.

#import <Cocoa/Cocoa.h>


@interface ShortcutsTableController : NSObject <NSTableViewDataSource> {
    IBOutlet NSTableView *shortcutsTable;
    NSMutableArray *shortcutsList;
}

- (int) numberOfRowsInTableView: (NSTableView*) tableView;
- (id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(int)row;

@property (assign) IBOutlet NSTableView *shortcutsTable;
- (void)setUpTable;

@end

And this is the implementation file ShortcutsTableController.m.

#import "ShortcutsTableController.h"

@implementation ShortcutsTableController

@synthesize shortcutsTable;

- (void)setUpTable {

    shortcutsList = [[NSMutableArray alloc] init];

    NSDictionary *dict1 = [NSDictionary dictionaryWithObjectsAndKeys:
                           @"blabla", @"nameColumn", 
                           @"Bla bla bla", @"shortcutColumn",
                           @"Ribla", @"actionColumn", nil];

    [shortcutsList addObject:dict1];

    [shortcutsTable setDataSource:self];
    [shortcutsTable reloadData];
}

-(int) numberOfRowsInTableView: (NSTableView *) tableView {
    return [shortcutsList count];
}

- (id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(int)row {

    if (row != -1)
     return [[shortcutsList objectAtIndex:row] objectForKey:[tableColumn identifier]];

    return nil;
}

@end

But when i try to build nothing appears in the NSTableView. No errors, no warnings. Note that I call setUpTable from within the Delegate Class Method awakeFromNib.

Is there something I am doing wrong? Thank you for you help.

—Albé

UPDATE. Added lines @property (assign) IBOutlet NSTableView *shortcutsTable; in header and @synthesize shortcutsTable; in implementation. Nothing changes. :(

A: 

How many objects in shortcutsList?

Try to iterate through the dictionary anywhere else but the datasource methods to see if its displaying data correctly.

Also, have you set the controller as the datasource of that table in IB or manually in your awakeFromNib?

Rui Pacheco
Manually in `setUpTable`: `[shortcutsTable setDataSource:self];`, but I connected also in Nib (CTRL drag from NSTableView to the controller, than I selected dataSource).
Alberto
I tried also to add a NSLog() to the tableView method, but in the console nothing appears. It seems that it is never used, even when i call reloadData.
Alberto
Is the controller the delegate for the table?
Rui Pacheco
Yes. The table is updated only when I reload from the init method of the controller.
Alberto
A: 

Based on the results of your NSLog statement in setUpTable:, you don't have your IBOutlet set up. Perhaps you did, but it somehow got lost (undo, overwrite, accidentally deleted it, etc). You'll need to go back to Interface Builder and re-establish the connection between ShortcutsTableController and the NSTableView in the xib.

Dave DeLong
Thank you for reply. It's very strange, I went back to Interface Builder and tried to re-establish the connection (which was already there). I tried also to remove the TableView and to re-add it, but it's always (null). IB says the controller and the NSTableView are connected, but in fact they aren't. :(
Alberto
Ok... The connection is now active (I don't know why :O). The value is not (null), but nothing appears in the table. If I add the elements and reload during init all goes as expected, but if I add items to the array after init, nothing changes.
Alberto
A: 

Are you calling [table reloadData] when you update shortcutList?

Rui Pacheco
Yes I am calling it. :-)
Alberto
A: 

Try adding

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

to your code where 1 is the number of sections you have in your table

James Raybould