0

My question is relevant to this one posted on StackOverflow.

similar question

In iOS, NSFetchedResultController has section parameter which can get data from Core Data when an attribute is set as section sectionNameKeyPath: while fetching attributes.

In Cocoa, NSTableView use Floating Group Rows to show section. My managed object model contains two entity.

Entity 1

Entity: Author

Attribute: name

Entity 2

Entity: Book

Attribute: title

Relationship booksByAuthor is ToMany.

I want to show Author name in NSTable floating group row followed by Book titles by them in the row below.

To set group cell, I am using following tableView delegate methods

-(NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row;

-(BOOL)tableView:(NSTableView *)tableView isGroupRow:(NSInteger)row;

-(CGFloat)tableView:(NSTableView *)tableView heightOfRow:(NSInteger)row;

I am setting table view data using NSArrayController.

I searched for tutorials for this but there is not much available for Cocoa. I am new it so bear with me. I am stuck at this, could anyone help me on this. Do I need to alter my model object.

Edit 1

I tried this approach but the result is jumbled

-(NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row{
NSArray *bookTitleArray = [[self.bookArrayController arrangedObjects]valueForKey:@"title"];
NSArray *fromAuthorArray = [[self.bookArrayController arrangedObjects]valueForKey:@"fromAuthor"];
Author *authorName = fromAuthorArray[row];

NSMutableArray *testArray = [[NSMutableArray alloc]init];
for (Author *auth in fromAuthorArray) {
    [testArray addObject:auth];
    for (Book *book in auth.books) {
        if (book.fromAuthor.name == auth.name) {
            [testArray addObject:book.title];
            
        }
    }
    
}


if ([testArray[row]isKindOfClass:[Author class]] ){
    NSTableCellView *newCell = [tableView makeViewWithIdentifier:@"authorCell" owner:self];
    
    [newCell.textField setStringValue:authorName.name];
    return newCell;
}else if (![testArray[row]isKindOfClass:[Book class]]){
    NSTableCellView *cellView = [tableView makeViewWithIdentifier:@"titleCell" owner:self];
    [cellView.textField setStringValue:bookTitleArray[row]];
    
    return cellView;
}
return nil;
}


-(BOOL)tableView:(NSTableView *)tableView isGroupRow:(NSInteger)row{
    NSArray *fromAuthorArray = [[self.bookArrayController arrangedObjects]valueForKey:@"fromAuthor"];
    NSMutableArray *testArray = [[NSMutableArray alloc]init];
    
for (Author *auth in fromAuthorArray) {
    [testArray addObject:auth];
    for (Book *book in auth.books) {
        if (book.fromAuthor.name == auth.name) {
            [testArray addObject:book.title];
            
        }
    }
    
}



if ([testArray[row]isKindOfClass:[Author class]]) {
    return YES;
}else if ([testArray[row]isKindOfClass:[Book class]]){
    return NO;
}
return NO;
}

Result:

Screenshot of NSTableView

Community
  • 1
  • 1
Napier
  • 105
  • 8
  • Show `arrangedObjects`-array creating/sorting code. – Daniyar Jan 13 '16 at 13:55
  • @Astoria, I have updated my question. In storyBoard I have set managedObjectContext and Entity. – Napier Jan 13 '16 at 14:54
  • This similar question has an answer: [Sectioned NSTableView using NSArrayController](http://stackoverflow.com/questions/5443113/sectioned-nstableview-using-nsarraycontroller) – Willeke Jan 15 '16 at 14:13

0 Answers0