Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > NSTableView and adding entries...

NSTableView and adding entries...
Thread Tools
Thinine
Mac Elite
Join Date: Jul 2002
Status: Offline
Reply With Quote
Jul 26, 2004, 12:48 AM
 
I'm trying to do the challenge at the end of chapter 6 in Hillegass' book and need a little help. I'm trying to add an entry to a table view and can't figure out why it's not working. I know the interface is connected correctly and I've entered to code he gave me correctly, so I'm not sure where to go from there.

I've made connections between the buttons and their respective actions, as well as the table view and it's outlet. Is there anything else I need to do?

So, in general, how would I add an entry to a table view?
     
Angus_D
Addicted to MacNN
Join Date: Mar 2000
Location: London, UK
Status: Offline
Reply With Quote
Jul 26, 2004, 07:13 AM
 
In general, you'd do whatever you need to do to its dataSource, then call -reloadData or -noteNumberOfRowsChanged.
     
techtrucker
Senior User
Join Date: Feb 2003
Location: USA
Status: Offline
Reply With Quote
Jul 26, 2004, 08:51 AM
 
Thinine,

Which edition of the book are you working with?
MacBook 2.0 160/2GB/SuperDrive
Lots of older Macs
     
techtrucker
Senior User
Join Date: Feb 2003
Location: USA
Status: Offline
Reply With Quote
Jul 26, 2004, 09:08 AM
 
I looked at the first edition and chapter 6 is about Archiving, so I'm going to guess it's the second edition that you're using. Chapter 6 gets into the new Bindings and controller classes. The challenge I believe you're referring to is where he has you redo the application the "old way" without bindings and array controllers.
All I can say is that you need to double check all of your IB connections. Make sure the data source is specified, and that all the columns have the correct names. It's relatively easy to miss something along the way. Or, as Angus_D mentioned you could just not be reloading the table.
I worked through the same project back when the first edition of the book came out. I found the project and zipped it up for you if you'd like to take a look at it:

http://homepage.mac.com/stevewoodward/FileSharing4.html

If you need it in an earlier format (it's updated to XCode) let me know.

Steve W
( Last edited by techtrucker; Jul 26, 2004 at 09:13 AM. )
MacBook 2.0 160/2GB/SuperDrive
Lots of older Macs
     
hayesk
Guest
Status:
Reply With Quote
Jul 26, 2004, 09:08 AM
 
Have you checked the web site?

Go here and type the page number in the form:

http://www.bignerdranch.com/products/cocoa1.shtml
     
Thinine  (op)
Mac Elite
Join Date: Jul 2002
Status: Offline
Reply With Quote
Jul 26, 2004, 01:11 PM
 
Yes, it is the second edition of the book and yes, it's redoing the RaiseMan application without using bindings. I've entered the code given in the book (added a few NSLog's to make sure my interface was working) but I can't seem to make new entries in the table view after clicking the Add Person button. I've checked out the BNR site and looked around cocoadev and from what I see, I've implemented everything I need to. But I can't seem to create new entries in the tableView. Should the code he gives us work, or do I need to add something? If I need to add something, can I get a hint at least?

MyDocument.h
Code:
#import <Cocoa/Cocoa.h> @interface MyDocument : NSDocument { NSMutableArray *employees; IBOutlet NSTableView *tableView; } - (IBAction)createEmployee:(id)sender; - (IBAction)deleteSelectedEmployees:(id)sender; @end
MyDocument.m
Code:
#import "MyDocument.h" #import "Person.h" @implementation MyDocument - (id)init { [super init]; employees = [[NSMutableArray alloc] init]; return self; } //Action Methods - (IBAction)deleteSelectedEmployees:(id)sender { NSIndexSet *rows = [tableView selectedRowIndexes]; if ([rows count] > 0) { unsigned int row = [rows lastIndex]; while (row != NSNotFound) { [employees removeObjectAtLastIndex:row]; row = [rows indexLessThanIndex:row]; } [tableView reloadData]; }else { NSBeep(); } } - (IBAction)createEmployee:(id)sender { Person *newEmployee = [[Person alloc] init]; [employees addObject:newEmployee]; [newEmployee release]; [tableView reloadData]; NSLog(@"Tried to create employee"); int rows = [tableView numberOfRows]; NSLog(@"tableView has %d rows", rows); } - (int)numberOfRowsInTableView:(NSTableView *)aTableView { return [employees count]; } - (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(int)rowIndex { NSString *identifier = [aTableColumn identifier]; Person *person = [employees objectAtIndexRow:rowIndex]; return [person valueForKey:identifier]; } - (void)tableView:(NSTableView *)aTableView setObjectValue:(id)anObject forTableColumn:(NSTableColumn *)aTableColumn row:(int)rowIndex { NSString *identifier = [aTableColumn identifier]; Person *person = [employees objectAtIndex:rowIndex]; [person setValue:anObject forKey:identifier]; } - (NSString *)windowNibName { // Override returning the nib file name of the document // If you need to use a subclass of NSWindowController or if your document supports multiple NSWindowControllers, you should remove this method and override -makeWindowControllers instead. return @"MyDocument"; } - (void)windowControllerDidLoadNib:(NSWindowController *) aController { [super windowControllerDidLoadNib:aController]; // Add any code here that needs to be executed once the windowController has loaded the document's window. } - (NSData *)dataRepresentationOfType:(NSString *)aType { // Insert code here to write your document from the given data. You can also choose to override -fileWrapperRepresentationOfType: or -writeToFile:ofType: instead. return nil; } - (BOOL)loadDataRepresentation:(NSData *)data ofType:(NSString *)aType { // Insert code here to read your document from the given data. You can also choose to override -loadFileWrapperRepresentation:ofType: or -readFromFile:ofType: instead. return YES; } @end
     
techtrucker
Senior User
Join Date: Feb 2003
Location: USA
Status: Offline
Reply With Quote
Jul 26, 2004, 04:04 PM
 
I'm not at home right now, but I am pretty sure he points you in the right direction but doesn't give you 100% of the code...
MacBook 2.0 160/2GB/SuperDrive
Lots of older Macs
     
techtrucker
Senior User
Join Date: Feb 2003
Location: USA
Status: Offline
Reply With Quote
Jul 27, 2004, 08:04 AM
 
Try putting a call to [self createEmployee] in init and see how many rows get logged...
MacBook 2.0 160/2GB/SuperDrive
Lots of older Macs
     
Thinine  (op)
Mac Elite
Join Date: Jul 2002
Status: Offline
Reply With Quote
Jul 27, 2004, 01:55 PM
 
Nope, doing that breaks the app entirely (selector not recognized error at run time). I think my problem is that I'm never actually adding a row to tableView. The log statement in my code shows that size never increases, while the employees array does, as evidenced by another log statement (not in the code above). So obviously I'm missing something when it comes to adding a row to tableView, and I can't figure out what that is.
     
techtrucker
Senior User
Join Date: Feb 2003
Location: USA
Status: Offline
Reply With Quote
Jul 27, 2004, 03:52 PM
 
I'm thinking it's a missing connection in IB. Make sure your connection to the table is to the table view, not the scroll view (that one bit me in the beginning). If you like send the project over to me and I'll see if I can figure it out.

stevewood66 at comcast dot net
MacBook 2.0 160/2GB/SuperDrive
Lots of older Macs
     
Thinine  (op)
Mac Elite
Join Date: Jul 2002
Status: Offline
Reply With Quote
Jul 27, 2004, 07:11 PM
 
Nope, it's all connected right. That's the first thing I checked. My project is available here. Finder-zipped archive.
     
Ambit
Fresh-Faced Recruit
Join Date: Jul 2004
Status: Offline
Reply With Quote
Jul 28, 2004, 06:25 AM
 
Hello,

I'm new to Cocoa and have only just started the Hillegass book myself, after going through Kochan's excellent book on Objective-C, so please feel free to tell me to shut up and leave the replies to people who know what they're talking about. However, I was just wondering about this segment of code in createEmployee:

Person *newEmployee = [[Person alloc] init];
[employees addObject:newEmployee];
[newEmployee release];
[tableView reloadData];

There is no data in the object you have added to the array, so I was wondering if this could be the problem (ie. although the object gets added to the array, the table view hasn't got anything to display). Just as a test, have you tried hard coding the instance variables for newEmployee before adding it to the employees array?

Like I say, I am probably waaaay off the mark here, but I was interested in your post because I'm going to be facing the same challenge myself in a couple of days. Feel free to ignore this completely.

Cheers,
KB
     
techtrucker
Senior User
Join Date: Feb 2003
Location: USA
Status: Offline
Reply With Quote
Jul 28, 2004, 07:08 AM
 
Ambit,

In this case the book is having you set initial values in the init method of the Person class, as in:

- (id)init
{
[super init];
[self setExpectedRaise:5.0];
[self setPersonName:@"New Person"];
return self;
}

So each new Person you create starts out with those values.


Thinine,

I took a brief look at your project and so far I can't tell what's missing. I did verify that the array was being added to...later on when I'm back home I'm going to dig into it deeper.

Steve W
MacBook 2.0 160/2GB/SuperDrive
Lots of older Macs
     
Chuckit
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status: Offline
Reply With Quote
Jul 28, 2004, 09:38 PM
 
The immediate problem you're seeing is in your nib. You have a second, useless instance of MyDocument alongside File's Owner. The tableview is using File's Owner as its data source (as it should be), but the two buttons are connected to the phantom instance, and the tableView outlet in File's Owner isn't connected to anything at all.

However, the app still won't work quite right after you fix this. That part should be pretty easy, though. I recommend you look at your error messages.
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
     
Thinine  (op)
Mac Elite
Join Date: Jul 2002
Status: Offline
Reply With Quote
Jul 29, 2004, 01:15 AM
 
Yep, that was it. I knew it was something stupid, just not how stupid it was. Thanks.

techtrucker, thanks for trying and sorry to waste your time.
     
techtrucker
Senior User
Join Date: Feb 2003
Location: USA
Status: Offline
Reply With Quote
Jul 29, 2004, 11:27 AM
 
No problem at all, I just wish I had discovered the problem first! I got bogged down yesterday in a web site design gone wrong...anyone here good with ImageReady and PSD web templates?
At any rate, I should have noticed the instance of MyDocument which was not needed. I'll use the lame excuse that I haven't done a document based app in a couple of years.

Steve W
MacBook 2.0 160/2GB/SuperDrive
Lots of older Macs
     
wataru
Addicted to MacNN
Join Date: Oct 2001
Location: Yokohama, Japan
Status: Offline
Reply With Quote
Aug 7, 2005, 03:02 PM
 
I just went through this challenge, not realizing that the code he gives you on page 136-7 basically does everything for you except sorting and disabling the Delete button. The challenge is way too easy if you copy his code, but he says it's "to get you started," and at this point we haven't learned about NSIndexSet. So what gives?
     
PBG4 User
Senior User
Join Date: Feb 2001
Location: Deer Crossing, CT
Status: Offline
Reply With Quote
Aug 8, 2005, 12:55 PM
 
Originally Posted by techtrucker
Try putting a call to [self createEmployee] in init and see how many rows get logged...
This call should probably be [self createEmployee:self] as the method is looking for a sender object.
20" iMac G5! :D AND MacBook 1.83GHz!
Canon Digital Rebel Kit + 75 - 300mm lens. Yum Yum! :D
Check out my OS X Musical Scales program
     
Chuckit
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status: Offline
Reply With Quote
Aug 8, 2005, 02:52 PM
 
Er…the problem was solved, like, a year ago. Wataru revived it to ask why the challenge is so pointless. (I don't know. I never used the Hillegass book.)
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
     
PBG4 User
Senior User
Join Date: Feb 2001
Location: Deer Crossing, CT
Status: Offline
Reply With Quote
Aug 9, 2005, 03:25 PM
 
Originally Posted by Chuckit
Er…the problem was solved, like, a year ago. Wataru revived it to ask why the challenge is so pointless. (I don't know. I never used the Hillegass book.)
Silly me, I just looked at the date/time stamp on the post previous to mine.

My correction still stands though and just might help someone who digs up this thread in a search next year.
20" iMac G5! :D AND MacBook 1.83GHz!
Canon Digital Rebel Kit + 75 - 300mm lens. Yum Yum! :D
Check out my OS X Musical Scales program
     
Chuckit
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status: Offline
Reply With Quote
Aug 9, 2005, 03:28 PM
 
True 'nuff.
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
     
   
 
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Top
Privacy Policy
All times are GMT -4. The time now is 06:35 PM.
All contents of these forums © 1995-2017 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.8 © 2000-2017, Jelsoft Enterprises Ltd.,