Reputation: 4391
I know that table sources need a data source to hold the data that the tableview will display. Lets' say that I'm going to make my AppController be the data source of my tableview and that I make the connection in interface builder. My question is since my actual data is going to be stored in an array,let's call it myArray, when I set the data source in code should I do this
[tableView setDataSource:myArray]; or this [tableView setDataSource:self];
I'm confused about this. setting the data source with the keyword "self" would set it to the AppController if I'm not mistaken.
Upvotes: 3
Views: 5316
Reputation: 46020
A table view data source must conform to the NSTableViewDataSource
protocol (called NSTableDataSource
prior to 10.6).
NSArray
does not conform to this protocol, so you can't use it as a data source.
You need to implement the required protocol methods in your AppController
object and assign your AppController
object as the table's data source.
- (void)applicationDidFinishLaunching:(NSNotification*)notification
{
[tableView setDataSource:self];
}
- (NSInteger)numberOfRowsInTableView:(NSTableView *)aTableView
{
return [myArray count];
}
- (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex
{
return [myArray objectAtIndex:rowIndex];
}
Upvotes: 7