Jump to content

Arrays of Objects


3 posts in this topic

Recommended Posts

So I'm rather new to objective c (two three days or so). I've done the target-action as well as binding thing for interface builder to communicate with xcode and that's all fine. Here's my problem:

- I have a class - say it's called Item (properties name and price)

- I have an interface (table view, button, two text fields)

- I want to enter the name and price in the fields - create and item object and have it added to teh list of item objects in the table view.

 

I have no idea how to use the array - set classes in objC. I was thinking maybe I need a test (or controller) class that acts inbetween my class (item) and the nib file - and which contains the array of items...? But I can't be sure.

 

Oh, I'm new to objC but I've done (chronologically) basic, pascal, visual basic, java, c, c++ and csharp . All to rather annoyingly advanced levels.

 

HELP!

Link to comment
Share on other sites

Well, the best way to do this is to of course, create a controller class. However, your 'item' class doesn't necessarily have any purpose. You should just create either two NSMutableArrays (one for name, one for price) or an NSMutableDictionary. Then under the datasource method in your controller (the datasource for the table), just set the [array objectAtIndex:row]; as the data.

 

I suggest for some explanations on NSTableViews to go to cocoadev (specifically for table views: this one).

 

To get the datasource to return two columns of data, do something like:

- (id)tableView:(NSTableView *)tableView
  objectValueForTableColumn:(NSTableColumn *)tableColumn
  row:(int)row
{
id value = nil;

if ([[tableColumn identifier] isEqualToString:@"first column"])
{
	value = [arrayA objectAtIndex:row];
}
else
{
	value = [arrayB objectAtIndex:row];
}

return value;
}

where arrayA would be your name array, and arrayB would be your price array. Make sure the headers titles in IB are set, and replace if ([[tableColumn identifier] isEqualToString:@"first column"]) with if ([[tableColumn identifier] isEqualToString:@"<title of the name column>"]). To add objects to these arrays, do [arrayName addObject:@"object"];. To get the value of a text field, and add it to an array (say to the name array), you would do something like [arrayA addObject:[nameField stringValue]];.

Link to comment
Share on other sites

Well, the best way to do this is to of course, create a controller class. However, your 'item' class doesn't necessarily have any purpose. You should just create either two NSMutableArrays (one for name, one for price) or an NSMutableDictionary.

 

Having the item class is a good thing. It aint bad to have multiple small classes. Storing data like that using arrays into a controller class is very dirty. Try keeping your GUI (controller) seperated from the 'backend' as much as possible.

 

BTW, an NSTableView also requires implementing the

 

- (NSInteger)numberOfRowsInTableView:(NSTableView *)aTableView;

 

property in your datasource.

Link to comment
Share on other sites

 Share

×
×
  • Create New...