2012-10-07 204 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

做到這一點:)如果你接受寫回答,它會鼓勵我們在未來幫助你:) –

相關問題