Cocoa - View-Based NSTableView, using one cell in multiple tables
Asked Answered
J

1

4

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

Johannessen answered 24/11, 2011 at 20:20 Comment(4)
Sure they are NSTableView and not UITableView?Tangent
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.Johannessen
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?Johannessen
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.Voiture
D
1

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

Disorderly answered 28/11, 2011 at 6:2 Comment(5)
Alex, thanks for your response. However, I've already got it working as in your example, where the Table View Cell (as in your screenshot) is already associated with / part of table in IB. What i'm trying to do is to move that cell outside of any one individual table, so that the same cell layout (which consists of many different controls) can be used in multiple tables without re-laying it out each time. I did try using the same code, but nothing appears (presumably because it doesn't know where to look). I'm starting to come to the assumption that it might simply not be possible...Johannessen
You may need to subclass nsview. Its really not hard. Otherwise if you must use ib you can make a separate nib for it and like in the example you linked use init with nib nameDisorderly
Ok thanks, I'll have to have a better read around subclassing NSView. I did have one test where it was a separate Nib, but didn't work - but then I don't think I was subclassing (or if I was it was because I'd just copied some code from somewhere, and didn't understand it). It'll take me a couple of days to try it, but I'll feedback how I got on if / when I find a solution. Cheers.Johannessen
Hey David, I'm trying to do the same thing as you and having trouble figuring out how to do this (seemingly simple) thing. How did you end up solving this problem? Cheers.Bragg
@Bragg In case this helps anyone, the tableView:viewForTableColumn:row: method is in the table view delegate, not the data source.Brio

© 2022 - 2024 — McMap. All rights reserved.