簡體   English   中英

自定義NSTableView與自定義NSTableCellView?

[英]Custom NSTableView with custom NSTableCellView?

我想用自定義NSTableCellViews創建一個NSTableview。

這就是我現在所擁有的:

  • 單元的nib文件(視圖nib),名為CustomCell.xib
  • 我的單元格的自定義類,名為CustomCell
  • 我的AppDelegate.m中的代碼:

這里我以編程方式創建表視圖:

    NSScrollView *tableContainer = [[NSScrollView alloc]initWithFrame:NSMakeRect(self.window.frame.size.width-TABLEWIDTH, 0, TABLEWIDTH, self.window.frame.size.height)];
    NSTableView *tableView = [[NSTableView alloc] initWithFrame:NSMakeRect(self.window.frame.size.width-TABLEWIDTH, 0, TABLEWIDTH, self.window.frame.size.height)];

    NSTableColumn *firstColumn = [[[NSTableColumn alloc] initWithIdentifier:@"firstColumn"] autorelease];
    [[firstColumn headerCell] setStringValue:@"First Column"];
    [tableView  addTableColumn:firstColumn];

    tableView.dataSource = self;
    tableView.delegate = self;
    [tableContainer setDocumentView:tableView];
    tableContainer.autoresizingMask = NSViewHeightSizable | NSViewMinXMargin;
    [self.window.contentView addSubview: tableContainer];

這是委托方法,我想把我的自定義單元格代碼:

- (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {


    // In IB the tableColumn has the identifier set to the same string as the keys in our dictionary
    NSString *identifier = [tableColumn identifier];

    if ([identifier isEqualToString:@"myCell"]) {

        // We pass us as the owner so we can setup target/actions into this main controller object
        CustomCell *cellView = [tableView makeViewWithIdentifier:identifier owner:self];
        // Then setup properties on the cellView based on the column
        cellView.textField.stringValue = @"Name";
        return cellView;
    }
    return nil;
}

在我的自定義單元格筆尖文件我已經迷上了我的自定義類細胞視圖稱為CustomCell其子類NSTableCellView。 我現在還沒有做任何其他步驟。 所以我的CustomCell.m只是默認的初始化代碼。 我沒碰過它。 我在我的nib文件中沒有做任何其他事情,所以我沒有更改文件的所有者或類似的東西,因為我真的不知道該怎么做。 任何人都可以幫忙嗎? 我查看了Apple文檔中的示例文件,但經過幾天的研究后,我還沒有找到任何解決方案。 如果你能幫助我,我真的很感激。

這就是我最終做的事情:

當然,您必須繼承NSTableCellView並將其返回,如下所示。 如果您熟悉iOS中的表視圖,則應熟悉以下方法:

- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView{
    //this will be called first. It will tell the table how many cells your table view will have to display
    return [arrayToDisplay count];

}

- (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {

    //this is called after the count of rows is set. It will populate your table according to the data your array to display contains

    [tableView setTarget:self];
    [tableView setAction:@selector(click)];

    NSString *identifier = [tableColumn identifier];

    if ([identifier isEqualToString:@"TheCell"]) {

        CustomCell *cellView = [tableView makeViewWithIdentifier:identifier owner:self];
        cellView.cellText.stringValue = [arrayToDisplay objectAtIndex:row];
        return cellView;
    }

    return nil;
}

選擇行時觸發的click方法如下所示:

-(void)click{

    int index = [table selectedRow];

    // Do something with your data 
    //e.g
    [[arrayToDisplay objectAtIndex:index] findHiggsBoson]; 

}

還有一些必須添加到NSTableView:

NSTableColumn *column = [[NSTableColumn alloc] initWithIdentifier:@"column"];
column.width = self.frame.size.width;
[tableView addTableColumn:column];

您不需要將NSTableView子類化為具有自定義NSTableViewCell子類。 您可以考慮使用基於視圖的表視圖...

暫無
暫無

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

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