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

淺唱寂寞╮ 提交于 2019-12-21 18:06:22

问题


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:


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:

Just replace "Automatic" with the identifier you are using



来源:https://stackoverflow.com/questions/8262136/cocoa-view-based-nstableview-using-one-cell-in-multiple-tables

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!