Custom NSTableView with custom NSTableCellView?

I would like to create an NSTableview with custom NSTableCellViews.

Here is what I have right now:

  • The 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 programmatically create my table view:

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 own 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 connected the cell view using my customCellCell class, which subclasses NSTableCellView . I haven’t done anything yet. So my CustomCell.m is just the default initialization code. I did not touch him. And I didn’t do anything in my nib file, so I did not change the owner of the file or anything like that, because I really do not know what to do. Can anyone help? I looked at sample files from the Apple documentation, but after several days of research, I did not find any solutions. I would really appreciate it if you could help me.

+6
source share
2 answers

Here is what I did:

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

 - (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 starts when a row is selected will look like this:

 -(void)click{ int index = [table selectedRow]; // Do something with your data //eg [[arrayToDisplay objectAtIndex:index] findHiggsBoson]; } 

And something to add to the NSTableView:

 NSTableColumn *column = [[NSTableColumn alloc] initWithIdentifier:@"column"]; column.width = self.frame.size.width; [tableView addTableColumn:column]; 
+3
source

You do not need to subclass NSTableView to create custom subclasses of NSTableViewCell. You might also consider presenting a table based on a view ...

+1
source

Source: https://habr.com/ru/post/927205/


All Articles