簡體   English   中英

如何將子類添加到NSTableCellView?

[英]How can I add subclass to NSTableCellView?

我在IB中向NSTable添加了一個圖像和文本表單元格視圖。 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; 
}

我發現textfield和imageView有默認插座,所以我可以使用cell.textFiled訪問這個textField對象並更改它的值。 這是我的問題,如果我在這個圖像和文本表格單元視圖中添加一個額外的TextField,在一列中有兩個TextField,那么如何獲取由我添加的第二個TextFiled,更改TextFiled的值?

正如它在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; 
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM