3

我在 IB 中向 NSTable 添加了一个图像和文本表单元格视图。文本表格单元格视图中有一个 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; 
}

我发现 textfield 和 imageView 有默认出口,所以我可以使用 cell.textFiled 访问这个 textField 对象并更改它的值。这是我的问题,如果我在这个图像和文本表单元格视图中添加一个额外的 TextField,那么一列中有两个 TextField,那么我怎样才能获得我添加的第二个 TextFiled,更改 TextFiled 的值?

4

1 回答 1

4

正如它在NSTableCellView 类参考页面中所说的那样

可以通过继承 NSTableCellView 并添加所需的属性并以编程方式或在 Interface Builder 中连接它们来添加其他属性。

创建你的 NSTableCellView 子类(比如“CustomTableCellView”),定义一个额外的文本字段出口属性(图像视图和第一个文本字段在超类中定义)。在 Interface Builder 中设置单元原型的类,并将附加的文本字段控件连接到您的属性。

在您的 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; 
}
于 2015-02-27T07:26:36.663 回答