简体   繁体   中英

How can I add subclass to NSTableCellView?

I add an Image & Text Table Cell View to NSTable in IB. There is a TextFiled and a ImageView in the Text Table Cell View, so my code looks like this:

- (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. 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?

As it said at the NSTableCellView Class Reference page

Additional properties can be added by subclassing NSTableCellView and adding the required properties and connecting them programmatically or in 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). Set the class of your cell prototype in the Interface Builder and connect the additional text field control to your property.

In your NSTableViewDelegate class:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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