Populating Two NSTableViews From Two Different NSMutableArrays In Same Class
I am having difficulty populating two TableViews with data from two different MutableArrays in one Class. I am parsing an *.xml document and want to put the d开发者_JAVA技巧ata into to different tableviews. In the 'Using A Table Data Source' chapter of the 'Table View Programming Guide' it states 'A data source object that manages several sets of data can choose the appropriate set based on which NSTableView object sent the message.' I understand this to mean I can populate two different tableviews from two different mutablearrays in the same class. I don't seem to be able to figure out how to do this as I can not find any more information other than what was quoted above.
My tableview population code is:
- (int)numberOfRowsInTableView:aTableView {
return [arrayPowerData count];
}
- (id)tableView:aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)row {
PowerData* dataPower = [arrayPowerData objectAtIndex:row];
return [dataPower valueForKey:[aTableColumn identifier]];
}
I know there is data in both arrays because I can switch the array variable in the above code and it populates the appropriate tableview.
[blockTableView reloadData];
[dataTableView reloadData];
I have tried the Array Controller / Bindings route but the TableViews do not get populated even though I do not get any error messages or warnings.
I am new to Cocoa / Objective-c Programming, and not really a programmer at all, so, any help and / or direction would be greatly appreciated.
Essentially what's happening here is that both tables are calling those functions when they populate. When this happens, they pass themselves as the table view parameter aTableView
. So in order to populate both using the same methods, you need to filter out which table view is currently calling the method. Here's the basic idea:
- (int)numberOfRowsInTableView:aTableView {
if (aTableView == tableview1) {
return [array1 count];
}
else if (aTableView == tableview2) {
return [array2 count];
}
}
-(id)tableView:aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)row {
if (aTableView == tableview1) {
//populate tableview1 with the corresponding array
}
else if (aTableView == tableview2) {
//populate tableview2 with the other array
}
}
精彩评论