objective-c - 使用自定义 NSTableCellView 自定义 NSTableView?

标签 objective-c macos cocoa nstableview nstablecellview

我想创建一个带有自定义 NSTableCellViews 的 NSTableview。

这是我现在拥有的:

  • 单元格的 nib 文件(查看 nib)称为 CustomCell.xib
  • 我的单元格的自定义类 CustomCell
  • 还有我的 AppDelegate.m 中的代码:

这里我以编程方式创建我的 TableView :

    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];

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

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

在我的自定义单元格的 nib 文件中,我将单元格 View 与名为 CustomCell 的自定义类连接起来,该类是 NSTableCellView 的子类。目前我还没有执行任何其他步骤。所以我的 CustomCell.m 只是默认的初始化代码。我没有碰过它。而且我没有在我的 nib 文件中做任何其他事情,所以我没有更改文件的所有者或类似的东西,因为我真的不知道该怎么做。 任何人都可以帮忙吗?我查看了 Apple 文档中的示例文件,但经过几天的研究,我没有找到任何解决方案。如果你能帮助我,我将不胜感激。

最佳答案

这就是我最终做的:

当然,你必须继承 NSTableCellView 并返回它,就像我在下面所做的那样。如果您熟悉 iOS 中的 TableView ,您应该熟悉以下方法:

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

单击选择一行时触发的方法看起来像这样:

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

关于objective-c - 使用自定义 NSTableCellView 自定义 NSTableView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12787906/

相关文章:

objective-c - 将 UISwitch 添加到 UINavigationItem

objective-c - 在 objective-c 中声明 swift 协议(protocol)委托(delegate)变量时出错

ios - 有什么方法可以防止 Xcode 对 Storyboard 进行愚蠢的更新?

c++ - Mac OS X : is it possible to imbue a non-main thread to become "The Main Thread" of a process?

iPhone 颜色选择器

iphone - 如何判断 UIView 是否可见并显示在屏幕上?

ios7 所有 searchviews 和 tableviews 关闭 20 像素

node.js - 无法从本地网络上的不同设备访问 Mac 上的 Node 服务器

python - 在 Python 中自动化 'enter' 键(在 Mac 上)

swift - 停留在 hasSpaceAvailable 并且无法从 NSInputStream 读取