Table View Data Source Question
Table View Data Source Question
- Subject: Table View Data Source Question
- From: email@hidden
- Date: Mon, 8 Oct 2001 02:14:03 -0700
I have a table view that I'd like the user to enter data into. The
problem I am having is with the input; it never seems to do anything. I
know that it is in the tableView:setObjectValue:forTableColumn:row
method and I tried to implement it, but to no avail. I studied the
documentation and understand what I need to do, but it just isn't
working.
If you're interested, let's move on.
Here's more specifically what happens. The app starts out with an empty
NSTableView. There are no rows and no columns. Then, with the press of a
button, a sheet comes down and the user enters the name of a column and
it is created. Then, with another button, a row is added. For the data
source, I was hoping to make an array containing dictionaries with a key
for each column header. Here's an example of what I mean in graphical
terms:
|- Name => Jane Doe
|- Email => email@hidden
|- Dictionary -|- Age => 43
Array --
|- Dictionary -|- Name => Joe Shmoe
|- Email => email@hidden
|- Age => 39
So that looks ok, right? Well, using NSLog(@"Array: %@", tableData)
during the tableView:setObjectValue:forTableColumn:row method, I
unvailed my greatest defeat. The app returns "Array: (null)". I know my
problem is that I can't seem to put my visualization into Obj-C. Here's
my code:
Header File:
-----------------------
#import <Cocoa/Cocoa.h>
@interface Controller : NSObject
{
IBOutlet id columnNameField;
IBOutlet id columnTypePull;
IBOutlet id newColumnSheet;
IBOutlet id mainWindow;
IBOutlet id tableView;
int rowCount;
NSMutableArray *tableData;
}
- (IBAction)endColumnSheet:(id)sender;
- (IBAction)makeNewEntry:(id)sender;
- (IBAction)showColumnSheet:(id)sender;
@end
--------------------------
showColumnSheet
--------------------------
- (IBAction)showColumnSheet:(id)sender
{
[NSApp beginSheet:newColumnSheet modalForWindow:mainWindow
modalDelegate:self didEndSelector:NULL contextInfo:nil];
}
--------------------------
endColumnSheet
--------------------------
- (IBAction)endColumnSheet:(id)sender
{
NSTableColumn *newColumn = [[NSTableColumn alloc]
initWithIdentifier:[columnNameField stringValue]];
[[newColumn headerCell] setStringValue:[columnNameField
stringValue]];
[newColumnSheet orderOut:nil];
[NSApp endSheet:newColumnSheet];
[tableView addTableColumn:newColumn];
[newColumn release];
}
-------------------------
makeNewEntry
-------------------------
- (IBAction)makeNewEntry:(id)sender
{
NSMutableDictionary *newDict = [[NSMutableDictionary alloc] init];
rowCount++;
[tableData addObject:newDict];
[newDict release];
[tableView reloadData];
}
-------------------------
objectValueForTableColumn
-------------------------
- (id)tableView:(NSTableView *)aTableView
objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(int)rowIndex
{
return [[tableData objectAtIndex:rowIndex]
objectForKey:[aTableColumn identifier]];
}
--------------------------
setObjectValue
--------------------------
- (void)tableView:(NSTableView *)aTableView setObjectValue:(id)anObject
forTableColumn:(NSTableColumn *)aTableColumn row:(int)rowIndex
{
NSLog(@"Array: %@", tableData);
[[tableData objectAtIndex:rowIndex] setObject:anObject
forKey:[aTableColumn identifier]];
return;
}
-------------------------
Thanks,
Sam