0

I have a NSTableView where items can be added and deleted. Once items have been added to the table, I would like those items to also show as items for an NSPopUpButton. I tried the addItemsWithTitles: method but it gives me an error.

#import "TableController.h"
#import "Favorites.h"


@interface TableController ()

@property NSMutableArray *array;
@property (weak) IBOutlet NSTableView *tableView;
@property (weak) IBOutlet NSPopUpButton *popButton;

@end


@implementation TableController

- (id)init {
    self = [super init];
    if (self) {
        _array = [[NSMutableArray alloc] init];
    }
    return self;
}

- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView {
    return [_array count];
}

- (id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {
    Favorites *fav = [_array objectAtIndex:row];
    NSString *ident = [tableColumn identifier];
    return [fav valueForKey:ident];
}

- (void)tableView:(NSTableView *)tableView setObjectValue:(id)object forTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {
    Favorites *fav = [_array objectAtIndex:row];
    NSString *ident = [tableColumn identifier];
    [fav setValue:object forKey:ident];
}

- (IBAction)add:(id)sender {
    [_array addObject:[[Favorites alloc] init]];
    [_tableView reloadData];
    [_popButton addItemsWithTitles:_array];
}

-(IBAction)delete:(id)sender {
    NSInteger row = [_tableView selectedRow];
    [_tableView abortEditing];
    if (row != -1) {
        [_array removeObjectAtIndex:row];
    }
    [_tableView reloadData];
}

@end

So I tried logging the objectAtIndex:0 for the array and didn't get a string but received some numbers instead:

Array string is <Favorites: 0x10013e820>

And also for reference my Favorites class is

#import "Favorites.h"

@interface Favorites ()

@property (copy) NSString *location;

@end


@implementation Favorites

- (id)init {
    self = [super init];
    if (self) {
        _location = @"City, State or ZIP";
    }
    return self;
}

@end
wigging
  • 8,492
  • 12
  • 75
  • 117
  • That's not numbers, that's an instance of the class, Favorites. I can't tell why you would get that without knowing the structure of _array and Favorites. What error do you get after addItemsWithTitles? – rdelmar Aug 20 '12 at 00:54
  • @rdelmar I get the following error: TableViewPopUpButton[18848:403] -[Favorites isEqualToString:]: unrecognized selector sent to instance 0x10013e820 – wigging Aug 20 '12 at 00:57
  • Ok, that's what you're seeing in your log also, that your array has instances of your class rather than strings. If you log[fav valueForKey:ident] does that give you what you expect? – rdelmar Aug 20 '12 at 01:01
  • @rdelmar Not sure where I would put the NSLog. It doesn't give me anything if placed in the (IBaction)add – wigging Aug 20 '12 at 01:05

1 Answers1

1

Your array has instances of your class, Favorites, and in your addItemsWithTiles: method you are adding those instances, not some string property of those instances, which is what I presume you want. If you use [_array valueForKey:@"whateverKeyHasYourStrings"] as the argument to addItemsWithTitles: instead of just _array, I think it will work ok.

rdelmar
  • 103,982
  • 12
  • 207
  • 218
  • using [_popButton addItemsWithTitles:[_array valueForKey:@"location"]]; is working – wigging Aug 20 '12 at 01:36
  • Would you happen to know how I can make the tableview item editable upon adding to the table? – wigging Aug 20 '12 at 01:38
  • I think you need to implement controlTextDidEndEditing: in your table view delegate class which should be called whenever you finish editing a text cell in your table. You can update the model with the new data in that method. – rdelmar Aug 20 '12 at 01:46