在我的应用程序中,我正在尝试创建一个自定义单元格,如下所示:
我知道如何使用iOS来做到这一点,我想通过使用OS X它应该是完全相同的。所以在界面构建器中我设计了表视图,但我可以想出如何创建自定义单元格。我试图在.xib中插入我设计表视图我需要的自定义组件(2 NSTextField
和2 NSImageView
),然后我创建了一个类"CustomCell.m"
和"CustomCell.h"
作为NSTableCellView
的子类,所以我尝试将我的组件连接到此class,但我无法添加它...为什么我无法将组件连接到“CustomCell”类?怎么了?你能帮我找到办法吗(也许是教程)?
为此,我只是按照我现在的方法为iOS创建自定义表格单元格
@interface InheritedTableCellview : NSTableCellView
@property (assign) IBOutlet NSTextField *secondTextField;
@property (assign) IBOutlet NSImageView *secondImageView;
@end
@implementation SRITableCellView
@end
在视图控制器中,实现以下数据源方法以显示所需的行数。
- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView {
return self.tableArray.count;
}
实现委托方法为每行设置图像和文本,
- (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
{
InheritedTableCellview *cellView = [tableView makeViewWithIdentifier:@"MainCell" owner:self];
cellView.backgroundStyle = NSBackgroundStyleDark;
cellView.textField.stringValue = self.tableArray[row][@"textValue1"];
cellView.imageView.image = [NSImage imageNamed:self.tableArray[row][@"image1"]];
cellView.secondTextField.stringValue = self.tableArray[row][@"textValue2"];
cellView.secondImageView.image = [NSImage imageNamed:self.tableArray[row][@"image2"]];
return cellView;
}
在xib中,选择要连接的自定义单元格,然后转到右侧面板(https://developer.apple.com/library/ios/recipes/xcode_help-general/Chapters/AbouttheUtilityArea.html)的“实用程序”区域中的Identity Inspector。将类更改为CustomCell类型。然后你应该能够将它连接到插座
从Lion开始,Apple不鼓励使用NSCell子类。您现在可以执行基于NSView的NSTableView,它更加灵活。
我已经通过在dataSource方法中创建NSViews来完成它们,但here's是用nib做的一个不错的选择。
另见apple docs。