Cocoa : Custom TableView cell?

1.5k Views Asked by At

I am not really familiar with tables, as I usually make games, but now I want to create a level builder where I need a table view with custom cells. I have created a nib file and I have subclassed NSTableCellView, but I don't know what to do next. All I have is:

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

}

If any, which delegate methods do I have to implement ? And how do I customize my cell WITH a nib file ?

1

There are 1 best solutions below

2
On

Do this in ur 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;
}

In 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:)