Search code examples
xcode4tableviewdatasourcecell

Please explain me how to control a Table View in Cocoa?


I search Google to find a good answer but the most tutorials are shown in previous Xcode Versions... Also, I don't want to drag-n-drop cells from the Interface Builder, but to control the Table View programmatically (from an NSObject subclass file).



What I currently do is this:


1. Create a file named tableController.h that is a subclass of NSObject. 2. I create an NSObject Object in my Nib File (and set it as a subclass of tableController).
3. I drag a Table View to my window.
4. I CTRL+Drag from the Table View to my tableController.h so to create the outlet "tableView"
5. I create these functions in the interface file:

-(int)numberOfRowsInTableView:(NSTableView *)cocoaTV;
-(id)tableView:(NSTableView *)cocoaTV:objectValueForTableCollumn:(NSTableColumn *)tableCollumn row:(int)row;

6. I implement the functions like this:

-(int)numberOfRowsInTableView:(NSTableView *)cocoaTV{
    return 5;
}
-(id)tableView:(NSTableView *)cocoaTV:objectValueForTableCollumn:(NSTableColumn *)tableCollumn row:(int)row{
    NSArray *tvArray = [[NSArray alloc]initWithObjects:@"1",@"2",@"3",@"4",@"5", nil];
    NSString *v = [tvArray objectAtIndex:row];
    return v;
}

Then I CTRL+Drag from the Object in the Interface Builder to the Table View to set the dataSource and to set it as delegate.

When I build and Run the App it shows that it has created the 5 Rows but in every cell in every column it says "Table View Cell".

Any help would be appreciated....


Solution

  • -(id)tableView:(NSTableView *)cocoaTV:objectValueForTableCollumn:(NSTableColumn *)tableCollumn row:(int)row is wrong.. i'm not sure how it compiles, to be honest (unless there was an error copy/pasting it). the method should look like:

    - (id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {
        NSArray *tvArray = [[NSArray alloc]initWithObjects:@"1",@"2",@"3",@"4",@"5", nil];
        NSString *v = [tvArray objectAtIndex:row];
        return v;
    }