NSTableView and NSMutableArray

Discussion in 'Mac Programming' started by darbravo, Apr 12, 2008.

  1. darbravo macrumors newbie

    Apr 11, 2008
    Hi all. I'm starting to develop in cocoa and have this problem that can't figure out by myself.

    I have a class with a NSMutableArray list. In the interface have a bunch of NSTextFields, an "Add Item" button and a table to display added items.
    In the controller, I had added the methods to create a new item and add it to the array. Also, I'd set the table datasource to the controller and added the two methods to retrieve the item count and to return a the value for a given row and column.

    The problem is that when I want to retrieve the items in the
    - (id) tableView: (NSTableView *)tv objectValueForTableColumn : row

    the item from the array list haves:
    NSString *name
    NSString *brand
    float quantity
    float price
    NSString *category

    but, when i retrieve it from the list to return the value of the row/column, the item haves all it's attributes but the tipe is NSRectSet.

    does anybody know what could be wrong?

    here is the code:


    	- (IBAction)addItem:(id)sender{
    		ListItem *item;
    		item = [[ListItem alloc] init];
    		[item setItemName: [nameField stringValue]];
    		[item setBrand:[brandField stringValue]];
    		[item setCategory:[categoryField stringValue]];
    		[item setQty:[qtyField floatValue]];
    		[item setPrice:[priceField floatValue]];
    		[shopList addItem: item];
    		[table setDataSource: self];
    		[table reloadData];
    		// clear text fields
    		[nameField setStringValue:@""];
    		[brandField setStringValue:@""];
    		[categoryField setStringValue:@""];
    		[qtyField setStringValue:@""];
    		[priceField setStringValue:@""];
    	- (int)numberOfRowsInTableView:(NSTableView *)tv {
    		return [[shopList items] count];
    	- (id)tableView:(NSTableView *)tv objectValueForTableColumn:(NSTableColumn *)tc row:(int)row {
    		ListItem *item = [shopList objectAtIndex:row];
    		NSString *returnValue;
    		if ([[tc identifier] isEqualToString: @"idCol"]){
    			returnValue = [NSString stringWithFormat:@"%i", row];
    		else if ([[tc identifier] isEqualToString:@"Category"]){
    			returnValue = [item category];
    		else if ([[tc identifier] isEqualToString:@"Name"]){
    			returnValue = [item itemName];
    		else if ([[tc identifier] isEqualToString:@"Brand"]){
    			returnValue = [item brand];
    		else if ([[tc identifier] isEqualToString:@"Quantity"]){
    			returnValue = [NSString stringWithFormat:@"%1.2f", [item qty]];
    		else if ([[tc identifier] isEqualToString:@"Price"]){
    			returnValue = [NSString stringWithFormat:@"%1.2f", [item price]];
    		return returnValue;
    thanks in advance!
  2. kainjow Moderator emeritus


    Jun 15, 2000
    Sounds like a memory management issue. Can you post the code where you're creating shopList? Also are you using garbage collection (if using 10.5)?
  3. darbravo thread starter macrumors newbie

    Apr 11, 2008
    It's solved! The problem was that when I was retrieving the recently added item, the method didn't casted it as a ListItem.

    Now my problem is that when I invoke the addItem method (see first post), and try to clear the textfields, the program crash...

    what could it be?

  4. kainjow Moderator emeritus


    Jun 15, 2000
    1) You shouldn't have to typecast it usually. If you do, that means there's a problem somewhere else
    2) If it's crashing then it means there's still a memory issue somewhere usually (depends on the crash). Can you post the crash report (relevant part?)
    3) Back to my original post, can you post the code where you create shopList?

    The code you posted looks fine. I think the problem is elsewhere.
  5. darbravo thread starter macrumors newbie

    Apr 11, 2008
    kainjow, thanks for your answers!

    Here is the method that creates the ShopList.
    It is invoked when you push a button and it takes the market and comment from two fields.

    It all happens in the same form where the items are added.

    	-(IBAction)newList: (id)sender{
    		// Initialize ShopList
    		shopList = [[ShopList alloc] init];
    		[shopList initialize];
    		[shopList setMarket:[marketField stringValue]];
    		[shopList setComments:[commentField stringValue]];
    		[dateField setStringValue:[[shopList date] descriptionWithCalendarFormat: @"%Y/%m/%d" timeZone: nil locale:[[NSUserDefaults standardUserDefaults] dictionaryRepresentation]]];
    		// Make other fields visible
    		[nameField setHidden: NO];
    		[nameLabel setHidden: NO];
    		[brandField setHidden: NO];
    		[brandLabel setHidden: NO];
    		[categoryField setHidden: NO];
    		[categoryLabel setHidden: NO];
    		[qtyField setHidden: NO];
    		[qtyLabel setHidden: NO];
    		[priceField setHidden: NO];
    		[priceLabel setHidden: NO];
    		[addItemButton setHidden:NO];
    		[tableScroll setHidden:NO];
    		[table setHidden: NO];
    		[marketField setEnabled:NO];
    		[commentField setEnabled:NO];
    		[dateField setEnabled:NO];
    		[newListButton setEnabled:NO];
  6. kainjow Moderator emeritus


    Jun 15, 2000
    Can you post the crash report? Have you looked at that? Unless your code has some crazy threadness going on, it should tell where the crash is coming from. From the above code it looks fine, so I'm thinking it's in one of your classes... but I don't want you to keep posting code :). I think the crash report is the key.
  7. darbravo thread starter macrumors newbie

    Apr 11, 2008
    I wanna make it public.

    Kainjow: you are the best! Thanks for ur help!
  8. Soulstorm macrumors 68000


    Feb 1, 2005
    Well, I had the same problem some time ago. Here is the problem:

    When you try to clear the values from the fields, the program crashes. That's because the function "objectValueForTableColumn" returns immutable objects. Let's say that you want to alter the "category" field for an item. You go into the field, and when you select it, the program displays it's value as an NSString. NSStrings cannot be changed. Use NSMutableStrings. I recommend changing both your class and the function to return mutable objects.

    At least that's what had happened to me some time ago...

Share This Page