基于可视 - 视图的NSTableView,在多个表中使用一个单元格 [英] Cocoa - View-Based NSTableView, using one cell in multiple tables

查看:322
本文介绍了基于可视 - 视图的NSTableView,在多个表中使用一个单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题。 [我可以找到的唯一的例子显示在WWDC 2011演示之一(Xcode 4中最大化生产力),但没有可用的源(这是一个应用程序叫Birdathon)。其他我想出的是为iOS,不翻译。]



基本上,我有一些基于视图的NSTableViews ,并且目前直接在列中布局我的NSTableCellView中的图像/文本字段。我有一个NSTableCellView的子类,它给我的插座,以分配值到我在该单元格内使用的每个文本字段。 DataSource和Delegate实现和工作正常 - TableView与我的自定义NSTableViewCell工作正常。



我的问题是我想在多个不同的表中使用相同的单元格。而不是每次都重新创建相同的布局,我觉得我应该能够绘制NSTableCellView只是一次在IB。 [ - 实际上,我提到的Birdathon例子似乎显示NSTableCellView是在它自己的NIB中布局的。]



我在许多地方找到了iOS的答案,例如:如何从Xib文件加载自定义UITableViewCells?



任何人都可以帮助我修改Cocoa on Mac?



感谢,



David

解决方案

像这样!

   - (NSInteger)numberOfRowsInTableView :( NSTableView *)aTableView {
return count;
}
- (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {
NSView * customView = [tableView makeViewWithIdentifier:@customview
owner:self];
...... //设置属性
return customView;在界面生成器中,将自定义单元格视图的标识符设置为customview,然后单击下一步它会自动创建!示例:





只需使用您使用的标识符替换自动


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

解决方案

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

这篇关于基于可视 - 视图的NSTableView,在多个表中使用一个单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆