Cocoa:自定义TableView单元格?

时间:2012-10-07 17:33:33

标签: macos cocoa nstableview nstablecellview

我并不熟悉桌子,因为我经常制作游戏,但现在我想创建一个关卡构建器,我需要一个带有自定义单元格的表视图。我创建了一个nib文件,并且我已经将NSTableCellView子类化了,但我不知道下一步该做什么。我只有:

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification{
    NSScrollView * tableContainer = [[NSScrollView alloc] initWithFrame:NSMakeRect(self.window.frame.size.width-TABLEWIDTH, 0, TABLEWIDTH, self.window.frame.size.height)];
    SpriteTable *sT = [[SpriteTable alloc]initWithFrame:NSMakeRect(self.window.frame.size.width-TABLEWIDTH, 0, TABLEWIDTH, self.window.frame.size.height)];
    NSTableView *tableView = [[NSTableView alloc] initWithFrame: sT.bounds];

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


}


- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView{

    return 4;
}



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

    // get an existing cell with the MyView identifier if it exists
    CustomCell *result = [tableView makeViewWithIdentifier:@"MyView" owner:self];


    // There is no existing cell to reuse so we will create a new one
    if (result == nil) {
        NSLog(@"result = nil");

        // create the new NSTextField with a frame of the {0,0} with the width of the table
        // note that the height of the frame is not really relevant, the row-height will modify the height
        // the new text field is then returned as an autoreleased object
        //result = [[[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 250, 70)] autorelease];

        // the identifier of the NSTextField instance is set to MyView. This
        // allows it to be re-used
        result.identifier = @"MyView";
    }

    // result is now guaranteed to be valid, either as a re-used cell
    // or as a new cell, so set the stringValue of the cell to the
    // nameArray value at row

    result.imageView.image = [NSImage imageNamed:NSImageNameHomeTemplate];

    // return the result.
    return result;

}

如果有的话,我必须实施哪些委托方法?如何使用nib文件自定义单元格?

1 个答案:

答案 0 :(得分:0)

在您的子视图中执行此操作 - >

@implementation suhasView 
@synthesize name,containerView;// container view contains ur subview
- (NSView*) myView 
{
  NSBundle *bundle = [NSBundle bundleForClass:[self class]];
  NSNib *theNib = [[NSNib alloc] initWithNibNamed:@"suhas"bundle:bundle];
 [theNib instantiateNibWithOwner:self topLevelObjects:nil];
 return containerView;
}

在Controller->

- (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn   *)tableColumn row:(NSInteger)row 
{ 
suhas *a=[[suhas alloc]initWithFrame:NSMakeRect(0,0, 40, 40)];
NSView * v = [a myView]; 
[a.name setStringValue:@"suhas"];
return v;
}...//sorry for my naming of the class:)