自定义NSTableView与自定义NSTableCellView?

the*_*tic 6 macos cocoa objective-c nstableview 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];
Run Code Online (Sandbox Code Playgroud)

这是委托方法,我想放置我的自定义单元格代码:

- (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;
}
Run Code Online (Sandbox Code Playgroud)

在我的自定义单元格笔尖文件我已经迷上了我的自定义类细胞视图称为CustomCell其子类NSTableCellView.我现在还没有做任何其他步骤.所以我的CustomCell.m只是默认的初始化代码.我没碰过它.我在我的nib文件中没有做任何其他事情,所以我没有更改文件的所有者或类似的东西,因为我真的不知道该怎么做.任何人都可以帮忙吗?我查看了Apple文档中的示例文件,但经过几天的研究后,我还没有找到任何解决方案.如果你能帮助我,我真的很感激.

the*_*tic 3

这就是我最终所做的:

当然,您必须继承 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;
}
Run Code Online (Sandbox Code Playgroud)

click选择行时触发的方法如下所示:

-(void)click{

    int index = [table selectedRow];

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

}
Run Code Online (Sandbox Code Playgroud)

还需要将一些内容添加到 NSTableView 中:

NSTableColumn *column = [[NSTableColumn alloc] initWithIdentifier:@"column"];
column.width = self.frame.size.width;
[tableView addTableColumn:column];
Run Code Online (Sandbox Code Playgroud)