简体   繁体   English

如何将子类添加到NSTableCellView?

[英]How can I add subclass to NSTableCellView?

I add an Image & Text Table Cell View to NSTable in IB. 我在IB中向NSTable添加了一个图像和文本表单元格视图。 There is a TextFiled and a ImageView in the Text Table Cell View, so my code looks like this: Text Table Cell View中有TextFiled和ImageView,所以我的代码如下所示:

- (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row{
    NSString *iden = [ tableColumn identifier ];
    if ([iden isEqualToString:@"MainCell"]) {
        NSTableCellView *cell = [ tableView makeViewWithIdentifier:@"MainCell11" owner:self ];
        [cell.textField setStringValue:@"123"];
        [cell.imageView setImage:[[NSImage alloc] initByReferencingFile:@"/Users/Pon/Pictures/17880.jpg"]];
        return cell;
    }
    return nil; 
}

I found that textfield and imageView has default outlet, so I can use cell.textFiled to visit this textField Object and change the value of it. 我发现textfield和imageView有默认插座,所以我可以使用cell.textFiled访问这个textField对象并更改它的值。 Here is my question, if I add an extra TextField to this Image & Text Table Cell View, there is two TextField in one column, so how can I get the second TextFiled which added by me, change the TextFiled's value? 这是我的问题,如果我在这个图像和文本表格单元视图中添加一个额外的TextField,在一列中有两个TextField,那么如何获取由我添加的第二个TextFiled,更改TextFiled的值?

As it said at the NSTableCellView Class Reference page 正如它在NSTableCellView类参考页面上所说的那样

Additional properties can be added by subclassing NSTableCellView and adding the required properties and connecting them programmatically or in Interface Builder. 可以通过继承NSTableCellView并添加所需属性并以编程方式或在Interface Builder中连接它们来添加其他属性。

Create your NSTableCellView-subclass (say 'CustomTableCellView'), define an additional text field outlet property (the image view and the first text field are defined in the super class). 创建您的NSTableCellView子类(比如'CustomTableCellView'),定义一个额外的文本字段出口属性(图像视图和第一个文本字段在超类中定义)。 Set the class of your cell prototype in the Interface Builder and connect the additional text field control to your property. 在Interface Builder中设置单元格原型的类,并将其他文本字段控件连接到您的属性。

In your NSTableViewDelegate class: 在您的NSTableViewDelegate类中:

- (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row{
    NSString *iden = [ tableColumn identifier ];
    if ([iden isEqualToString:@"MainCell"]) {
        CustomTableCellView *cell = [ tableView makeViewWithIdentifier:@"MainCell11" owner:nil ]; // use custom cell view class
        [cell.textField setStringValue:@"123"];
        [cell.imageView setImage:[[NSImage alloc] initByReferencingFile:@"/Users/Pon/Pictures/17880.jpg"]];
        [cell.addinitionalField setStringValue:@"321"]; // that is all
        return cell;
    }
    return nil; 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM