4
votes

I've got a problem. [for which the only example I can find was shown during one of the WWDC 2011 presentations ("Maximising Productivity in Xcode 4"), but there is no source available (it was an app called Birdathon). Everything else I come up with is for iOS, and doesn't translate across.]

Basically, I have some view-based NSTableViews, and currently lay out the image / text fields within my NSTableCellView directly in the column. I've got a subclass of NSTableCellView which gives me the outlets to assign values to each of the text fields I use within that cell. The DataSource and Delegate are implemented and working fine - the TableView with my custom NSTableViewCell works fine.

My problem is I'd like to use the same cell in multiple different tables. Rather than have to recreate the same layout each time, I feel I should be able to draw the NSTableCellView just once in IB. [- and indeed, the Birdathon example I mentioned seemed to show the NSTableCellView being laid out in it's own NIB.]

I've found the answer for iOS in many places, here for example: How do you load custom UITableViewCells from Xib files?

Can anyone help me modify that for Cocoa on Mac?

Thanks,

David

1
Sure they are NSTableView and not UITableView?Dimme
That's what I'm trying to say, possibly not very clearly - I want to do this in Cocoa for Mac with NSTableView, but the only examples I can find are for iOS (Cocoa Touch) and hence use UITableView. The reason for linking to the UITableView question is because that solution is what I need, but translated into NSTableView.DaveWalker
Sorry I'm being stupid. The Birdathon example is UITableView, which is not what I want. So the obvious next question then: is it at all possible to do what I want with NSTableView / Cocoa?DaveWalker
Have you tried to just put all your NSTables in the same .xib, where you also define your NSCellView? Then, you could re-use the same NSCellView in all your tables, without reaching out to other .xib files.Motti Shneor

1 Answers

1
votes

Like this!

- (NSInteger)numberOfRowsInTableView:(NSTableView *)aTableView {
    return count;
}
- (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row { 
    NSView *customView = [tableView makeViewWithIdentifier:@"customview"
                                                     owner:self];
    …… // set properties
    return customView;
}

In interface builder, set the identifier of your custom cell view to "customview" and it will automagically be created! Example:

set identifier in xcode

Just replace "Automatic" with the identifier you are using