2012-10-07 144 views
0

我对表格并不熟悉,因为我通常会制作游戏,但现在我想创建一个关卡生成器,我需要使用自定义单元格的表格视图。我创建了一个nib文件,并且我已经创建了NSTableCellView的子类,但我不知道下一步该怎么做。我所有的是:可可:自定义TableView单元格?

- (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文件定制我的单元格?

回答

0

在乌尔subview-做到这一点>

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

在控制器 - >

- (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:) 
+0

非常感谢,但我刚刚创建了一个表在我的窗口,它的工作,所以我想我不需要这个。但我会尝试,并接受你的答案,如果这个作品:) –

+0

做到这一点:)如果你接受写回答,它会鼓励我们在未来帮助你:) –

相关问题