开发者

Can't get NSTableView to display data

开发者 https://www.devze.com 2022-12-13 10:05 出处:网络
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 ShortcutsTa

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;

@pro开发者_如何学Cperty (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. :(


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.


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?


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


Try adding

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

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

0

精彩评论

暂无评论...
验证码 换一张
取 消