Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 自定义NSTableView与自定义NSTableCellView?_Objective C_Macos_Cocoa_Nstableview_Nstablecellview - Fatal编程技术网

Objective c 自定义NSTableView与自定义NSTableCellView?

Objective c 自定义NSTableView与自定义NSTableCellView?,objective-c,macos,cocoa,nstableview,nstablecellview,Objective C,Macos,Cocoa,Nstableview,Nstablecellview,我想创建一个具有自定义NSTableCellView的NSTableview 以下是我现在拥有的: 单元格的nib文件(查看nib),名为CustomCell.xib 我的单元格的自定义类称为CustomCell 以及myAppDelegate.m中的代码: 在这里,我以编程方式创建表视图: NSScrollView *tableContainer = [[NSScrollView alloc]initWithFrame:NSMakeRect(self.window.frame.si

我想创建一个具有自定义NSTableCellView的NSTableview

以下是我现在拥有的:

  • 单元格的nib文件(查看nib),名为CustomCell.xib
  • 我的单元格的自定义类称为CustomCell
  • 以及myAppDelegate.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;
}
在自定义单元格的nib文件中,我已将单元格视图与名为CustomCell的自定义类连接起来,该类是NSTableCellView的子类。到目前为止,我还没有做任何其他步骤。因此,我的CustomCell.m只是默认的初始化代码。我没碰它。我并没有在我的nib文件中做任何其他事情,所以我并没有改变文件的所有者或者类似的事情,因为我真的不知道该做什么。
有人能帮忙吗?我查看了苹果文档中的示例文件,但经过几天的研究,我还没有找到任何解决方案。如果你能帮助我,我将不胜感激

不需要子类NSTableView就可以拥有自定义的NSTableViewCell子类。
你也可以考虑使用基于视图的表视图……

< p>这是我最后做的:

当然,您必须子类化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;
}
当选择一行时触发的
单击
方法如下所示:

-(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,我想要一个自定义外观的单元格。因此,我设置了一个nib视图文件,其中只有这个NSTableCellView单元格。现在,我想通过编程使我的NSTableView显示nib文件中的那些特定单元格。然而,上面的代码并没有真正改变我表中的任何内容。有道理吗?你解决了吗?“我也遇到了同样的问题……”戈萨默:是的,我遇到了。请看下面的答案。我不久前就做过这件事,现在我已经添加了一些代码,所以如果您遇到错误,请告诉我;)