Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Apr 12, 2008, 04:05 PM   #1
darbravo
macrumors newbie
 
Join Date: Apr 2008
NSTableView and NSMutableArray

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:

ListController.m:

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!
darbravo is offline   0 Reply With Quote
Old Apr 12, 2008, 04:12 PM   #2
kainjow
Moderator emeritus
 
kainjow's Avatar
 
Join Date: Jun 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)?
kainjow is offline   0 Reply With Quote
Old Apr 12, 2008, 04:41 PM   #3
darbravo
Thread Starter
macrumors newbie
 
Join Date: Apr 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?

thanks!
darbravo is offline   0 Reply With Quote
Old Apr 12, 2008, 05:31 PM   #4
kainjow
Moderator emeritus
 
kainjow's Avatar
 
Join Date: Jun 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.
kainjow is offline   0 Reply With Quote
Old Apr 12, 2008, 06:49 PM   #5
darbravo
Thread Starter
macrumors newbie
 
Join Date: Apr 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.

Code:
	-(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];
	}
darbravo is offline   0 Reply With Quote
Old Apr 12, 2008, 10:08 PM   #6
kainjow
Moderator emeritus
 
kainjow's Avatar
 
Join Date: Jun 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.
kainjow is offline   0 Reply With Quote
Old Apr 16, 2008, 09:44 AM   #7
darbravo
Thread Starter
macrumors newbie
 
Join Date: Apr 2008
I wanna make it public.

Kainjow: you are the best! Thanks for ur help!
darbravo is offline   0 Reply With Quote
Old Apr 16, 2008, 11:39 AM   #8
Soulstorm
macrumors 68000
 
Soulstorm's Avatar
 
Join Date: Feb 2005
Quote:
Originally Posted by darbravo View Post
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?

thanks!
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...
Soulstorm is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
nsmutablearray tacotester1 iPhone/iPad Programming 6 Apr 10, 2014 05:18 PM
NSMutableArray always replacing last value. DavidBlack Mac Programming 16 Dec 15, 2013 04:49 PM
NSMutableArray only adds one object! Oneill iPhone/iPad Programming 5 Oct 23, 2013 06:42 PM
Objective-C help with NSMutableArray dancks Mac Programming 4 Apr 15, 2013 05:25 PM
NSArray to NSMutableArray larswik Mac Programming 3 Feb 19, 2013 11:10 PM

Forum Jump

All times are GMT -5. The time now is 04:02 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC