在NSTableView中创建自定义单元格 [英] Create a custom cell in a NSTableView

查看:287
本文介绍了在NSTableView中创建自定义单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程式中,我要建立一个自订的储存格,如下所示:

In my app I'm trying to create a custom cell like the follow:

我知道如何做到这一点与iOS,我想它应该是完全相同的使用OS X 。
所以在界面生成器中我设计了表视图,但我可以说明如何创建一个自定义单元格。我试图插入在.xib中,其中我设计的表视图的自定义组件我需要(2 NSTextField 和2 NSImageView ),然后我创建一个类CustomCell.mCustomCell.h code> NSTableCellView ,所以我试图连接我的组件到这个类,但我不能添加它...为什么我不能连接组件到CustomCell类?怎么了?你可以帮我找到一种方法来做这个(也许是一个教程)?

I know how to do that with iOS and I guess it should be quite the same by using OS X. So in interface builder I designed the table view but I can figure how to create a custom cell. I tried to insert in the .xib in which I designed the table view the custom component I need (2 NSTextField and 2 NSImageView), then I create a class "CustomCell.m" and "CustomCell.h" as subclass of NSTableCellView, so I tried to connect my component to this class, but I can't add it... Why I can't connect the component to "CustomCell" class? What's wrong? Can you help me to find a way to do this (maybe a tutorial too)?

为了做到这一点,我只是按照我现在为iOS创建自定义表格单元格的方法

To do this I just followed the method I now to create custom table cell for iOS

推荐答案


  1. 在Xib中,添加NSTableView并确保contentType是基于视图检查器窗格。

  2. 表列包含一个TableCellview,默认情况下包含一个TableViewCell。删除TableViewCell。

  3. 根据需要将NSTextFields和ImageViews拖放到TableCellview中。
    默认情况下,NSTableCellview支持1个Imageview和1个Textfield。如果你需要两个,继承NSTableCellview并为你的组件创建IBOutlet属性,并将IB中的NSTableCellview类改为InheritedTableCellview。

  1. In the Xib, add a NSTableView and make sure the contentType is View Based in the Attributes Inspector pane.
  2. The Table column contains a TableCellview, which contains a TableViewCell by default. Remove the TableViewCell.
  3. Drag the NSTextFields and ImageViews into the TableCellview as needed. By default, NSTableCellview supports 1 Imageview and 1 Textfield. If you need two of each, inherit NSTableCellview and create IBOutlet properties for your components and change the class of NSTableCellview in IB to InheritedTableCellview.

@interface InheritedTableCellview : NSTableCellView

@property (assign) IBOutlet NSTextField *secondTextField;
@property (assign) IBOutlet NSImageView *secondImageView;

@end

@implementation SRITableCellView

@end


  • 使用唯一字符串命名TableCellview的标识符。

  • Name the identifier of TableCellview with unique string.

    在视图控制器中,实现以下数据源方法显示所需的行数。

    In the view controller, implement the below datasource method for display the number of rows required.

    - (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView {
        return self.tableArray.count;
    }
    

    实现委托方法为每一行设置图像和文本, p>

    Implement the delegate method to set the image and Text for each row,

    - (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;
    

    }

    这篇关于在NSTableView中创建自定义单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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