Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom NSTableView with custom NSTableCellView?

I would like to create an NSTableview with custom NSTableCellViews.

Here is what I have right now:

  • A nib file for the cell (view nib) called CustomCell.xib
  • A custom class for my cell called CustomCell
  • And the code in my AppDelegate.m:

Here I create my table view programmatically:

    NSScrollView *tableContainer = [[NSScrollView alloc]initWithFrame:NSMakeRect(self.window.frame.size.width-TABLEWIDTH, 0, TABLEWIDTH, self.window.frame.size.height)];
    NSTableView *tableView = [[NSTableView alloc] initWithFrame:NSMakeRect(self.window.frame.size.width-TABLEWIDTH, 0, TABLEWIDTH, self.window.frame.size.height)];

    NSTableColumn *firstColumn = [[[NSTableColumn alloc] initWithIdentifier:@"firstColumn"] autorelease];
    [[firstColumn headerCell] setStringValue:@"First Column"];
    [tableView  addTableColumn:firstColumn];

    tableView.dataSource = self;
    tableView.delegate = self;
    [tableContainer setDocumentView:tableView];
    tableContainer.autoresizingMask = NSViewHeightSizable | NSViewMinXMargin;
    [self.window.contentView addSubview: tableContainer];

And here is the delegate method where I would like to put my custom cell code:

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


    // In IB the tableColumn has the identifier set to the same string as the keys in our dictionary
    NSString *identifier = [tableColumn identifier];

    if ([identifier isEqualToString:@"myCell"]) {

        // We pass us as the owner so we can setup target/actions into this main controller object
        CustomCell *cellView = [tableView makeViewWithIdentifier:identifier owner:self];
        // Then setup properties on the cellView based on the column
        cellView.textField.stringValue = @"Name";
        return cellView;
    }
    return nil;
}

In the nib file for my custom cell I have hooked up the cell view with my custom class called CustomCell which subclasses NSTableCellView. I have not done any other steps as for now. So my CustomCell.m is just default initialization code. I haven't touched it. And I did not do anything else in my nib file, so I did not change file's owner or anything like that because I don't really know what to do. Can anyone help out ? I looked at sample files from the Apple documentation, but after days of researching I have not found any solutions. I would really appreciate if you could help me.

like image 826
the_critic Avatar asked Oct 08 '12 19:10

the_critic


1 Answers

This is what I ended up doing :

Of course you have to subclass NSTableCellView and return it like I did below. If you are familiar with table views in iOS you should be familiar with methods like:

- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView{
    //this will be called first. It will tell the table how many cells your table view will have to display
    return [arrayToDisplay count];

}

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

    //this is called after the count of rows is set. It will populate your table according to the data your array to display contains

    [tableView setTarget:self];
    [tableView setAction:@selector(click)];

    NSString *identifier = [tableColumn identifier];

    if ([identifier isEqualToString:@"TheCell"]) {

        CustomCell *cellView = [tableView makeViewWithIdentifier:identifier owner:self];
        cellView.cellText.stringValue = [arrayToDisplay objectAtIndex:row];
        return cellView;
    }

    return nil;
}

And the click method that is triggered when a row is selected would look like this:

-(void)click{

    int index = [table selectedRow];

    // Do something with your data 
    //e.g
    [[arrayToDisplay objectAtIndex:index] findHiggsBoson]; 

}

And something that has to be added to the NSTableView:

NSTableColumn *column = [[NSTableColumn alloc] initWithIdentifier:@"column"];
column.width = self.frame.size.width;
[tableView addTableColumn:column];
like image 132
the_critic Avatar answered Sep 19 '22 21:09

the_critic