简体   繁体   中英

Using an array of arrays to populate NSTableView

I currently have a number of arrays, each containing show title, description and duration. I have them in a further 'shows' array and I'm using this array to populate my NSTableView . What I would like to do is extract the show title from each of my arrays for the first column of my table, the description for the second and so on.

The code I have at the moment though takes the first array in my array of arrays and populates column one, the second array for the second column etc. How would I amend what I have so far to get the table to populate correctly? I've tried to use indexOfObject in place of objectAtIndex however doing so throws and exception. Here's my (simplified) code:

AppDelegate.m

- (void)applicationDidFinishLoading:(NSNotification *)aNotification
{
    NSArray *show1 = [[NSArray alloc] initWithObjects:@"Title", @"A description", nil];
    NSArray *show2...
    NSArray *show3...
    NSArray *show4...

    self.array = [[NSMutableArray alloc] initWithObjects: show1, show2, show3, show4, nil];
    [self.tableView reloadData];
}

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

- (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
{
    NSString *identifier = [tableColumn identifier];

    if([identifier isEqualToString:@"title"]) {
        NSTableCellView *title = [tableView makeViewWithIdentifier:@"title" owner:self];
        title.textField.stringValue = [self.array objectAtIndex:0];
        return title;
    } else if {...}

    return nil;
}

Michele Percich's comment is the correct answer: [self.array objectAtIndex:0] will return the first shows array. What you want is "NSArray * show = [self.array objectAtIndex:row]'" to get the show and then "[show objectAtIndex:0]" to get that shows title. Just a suggestion but I'd use an NSArray of NSDictionary's where the keys are the column identifiers. Then you could just use "[self.array objectAtIndex:row] valueForKey:identifier];"

Note also that the method you're overriding expects an instance of NSView (or subclass) to be returned (read the notes in the NSTableView.h header). You may want to use the tableView:objectValueForTableColumn:row: method instead and just return the appropriate NSString (based on the row & column identifier).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM