Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSTableView Multiple Row Types

I'm trying to create a homework planner app that has two types of TableCellViews in a View Based NSTableView. One type is a narrow bar that just has a label of what subject the below homework is for, and the other type is a row to input homework items. (I'll include a screenshot below.)

My question is: when creating new rows in a TableView, how do you specify which type of row you'd like to create? I'm assuming it has something to do with identifiers, but I can't find any information on how to use them in this way.

This is basically how it would look:

like image 828
Matt Cooper Avatar asked Jan 12 '23 10:01

Matt Cooper


1 Answers

You are on the right track with the identifiers. Here's how you use them.

First setup your NSTableView with your specific row types (as you've probably already done). In the screenshot below I made one row with a title and description and another with a few buttons.

Two different table rows

Next, you need to setup the desired identifiers. Click the first row in Interface Builder and select the Identity Inspector. Pick a unique identifier for your first row. Do the same for the other(s).

Set the row identifier

Finally, in your implementation create a new row of a specific type using the following code:

TableViewController.m

#pragma mark - NSTableViewDelegate

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

  NSTableCellView *cell;

  if(someCondition == YES) {
    cell = [self.tableView makeViewWithIdentifier:@"ButtonRow" owner:self];
  } else {
    cell = [self.tableView makeViewWithIdentifier:@"TitleDescriptionRow" owner:self];
  }

  return cell;
}

If you're looking for a more in depth tutorial, check out Cocoa Programming L51 - View-Based NSTableView (YouTube video, not by me).

like image 185
Boy van Amstel Avatar answered Jan 22 '23 19:01

Boy van Amstel