PDA

View Full Version : How to setup sub-arrays?




Garrett
Aug 3, 2008, 03:28 AM
I have a UITableView reading from a NSMutableArray, which loads from a file at start, and saves at terminate.

I can insert rows just fine (which are the sections in the UITableView).

I am just trying to figure out how to add a something like this:
http://www.grabup.com/uploads/59d94da232a3b851f6fec75b58d52750.png

Where the headers (A, B, H) would be the car name (which I already can insert and have setup), and each entry for that car like the name inside of it.

Right now here is how I insert something into my row:
[mainDelegate.carsArray insertObject:newCar atIndex:[mainDelegate.carsArray count]];

I assume I would be looking at something like:
[mainDelegate.carsArray insertObject:newEntry atIndex:CAR-INDEX-HERE];

Thanks,
Garrett



sfwalter
Aug 3, 2008, 08:27 AM
Take a look at the TableViewSuite samples downloaded from Apple's Iphone dev site. It shows how to do exactly what you need.

Garrett
Aug 3, 2008, 12:49 PM
Take a look at the TableViewSuite samples downloaded from Apple's Iphone dev site. It shows how to do exactly what you need.

It does, your right, but it doesn't mean I can just understand it. I am looking for the bare bones that I need, not all this code with no comments in it.

FYI: I have downloaded this example 3 times before, it has done me no good.

Garrett
Aug 3, 2008, 03:45 PM
Sorry for acting hostile, anyhow.

I assume I have to use my tableIndex.row to tell it who it's parent is right? Maybe I am just asking for examples on setting up parent oriented arrays? :rolleyes:

tacoman667
Aug 3, 2008, 06:31 PM
sad up on sections. That is what those are. Indexpath.section is section and .rows are the rows inthat section. Have an array forsections and anarrayfor eachsection nested In the main array.

Garrett
Aug 3, 2008, 07:05 PM
sad up on sections. That is what those are. Indexpath.section is section and .rows are the rows inthat section. Have an array forsections and anarrayfor eachsection nested In the main array.

So I will have to do:

Gas_BuddyAppDelegate *mainDelegate = (Gas_BuddyAppDelegate *)[[UIApplication sharedApplication] delegate];
NSArray *newEntry = [[NSArray alloc] initWithObjects:@"June 30th, 2008", "1562", "14.4", nil];
[mainDelegate.carsArray insertObject:newEntry atIndex:[carIndex.section]];

:confused:

Do you have an example of your method? Also, would it be possible to set keys for each of the array values? So the first could be referenced by date, milage, gallons?

Sbrocket
Aug 3, 2008, 10:25 PM
Do you have an example of your method? Also, would it be possible to set keys for each of the array values? So the first could be referenced by date, milage, gallons?

Sounds like you want the key-value pairing of an NSDictionary. I'd look into that data structure if you want to access your data via keys.

Garrett
Aug 3, 2008, 10:27 PM
Sounds like you want the key-value pairing of an NSDictionary. I'd look into that data structure if you want to access your data via keys.

I am not worried about the keys right now. I just have no reference on how to add rows to "sections".

Sbrocket
Aug 3, 2008, 10:54 PM
I am not worried about the keys right now. I just have no reference on how to add rows to "sections".

Your UITableView's dataSource is sent tableView:cellForRowAtIndexPath: for every row in the table. The number of sections is determined by numberOfSectionsInTableView:, and the number of rows in each section by tableView:numberOfRowsInSection: (which are also messages sent to your dataSource). You use indexPath.section and indexPath.row to determine which data you should insert into the cell before you return it (in tableView:cellForRowAtIndexPath:).

If you need more explanation than that, I suggest you take a look at some of the sample code involving UITableView. If you can't figure out how to structure your data to do that, I suggest you check out an elementary programming data structures book from somewhere and review it.

Garrett
Aug 3, 2008, 10:57 PM
Your UITableView's dataSource is sent tableView:cellForRowAtIndexPath: for every row in the table. The number of sections is determined by numberOfSectionsInTableView:, and the number of rows in each section by tableView:numberOfRowsInSection: (which are also messages sent to your dataSource). You use indexPath.section and indexPath.row to determine which data you should insert into the cell before you return it (in tableView:cellForRowAtIndexPath:).

If you need more explanation than that, I suggest you take a look at some of the sample code involving UITableView. If you can't figure out how to structure your data to do that, I suggest you check out an elementary programming data structures book from somewhere and review it.
Again, I can do that just fine. I just can't insert or convert an array to:

- Scion tC
- Jeep

To:

- Scion tC
---- Entry
---- Entry
- Jeep
---- Entry
---- Entry

Once I can figure this out I can differentiate which is which, I just can't nest my arrays into a object!

Sbrocket
Aug 4, 2008, 02:03 AM
Well there are any number of ways to do it, but the first one that comes to mind is this:

Array of Sections (NSArray)
|_ Item 1 (NSDictionary)
|_ FirstSectionName (NSString) for key "SectionName"
|_ Array of Rows (NSArray) for key "RowsInSection"
|_ RowData1 (NSDictionary)
|_ Row1Date (NSString) for key "Date"
|_ Row1Mileage (NSString) for key "Mileage"
|_ Row1Gallons (NSString) for key "Gallons"
|_ RowData2 (NSDictionary)
|_ RowData3 (NSDictionary)
|_ Item 2 (NSDictionary)
|_ SecondSectionName (NSString) for key "SectionName"
|_ Array of Rows (NSArray) for key "RowsInSection"
|_ RowData1 (NSDictionary)
|_ RowData2 (NSDictionary)
|_ RowData3 (NSDictionary)
|_ RowData4 (NSDictionary)
|_ RowData5 (NSDictionary)

Of course, each dictionary at the bottom level of this structure would have the Date, Milegate, Gallons, and whatever other keys you need. I just left them out to avoid taking up tons of space.

Starting to get it now? Lay out the structure for your data based on how you need to use it, or alternatively however you think it would be most effectively accessed or stored.

tacoman667
Aug 4, 2008, 07:23 AM
NSArray Cars
Car1 object
Car2 object
Car3 object

NSArray Trucks
Truck1 object
Truck2 object

NSArray Automobiles
Cars
Trucks


At this point you will call it like this:



-(NSString*) tableView:tv nameForSection:section {
return [[NSArray objectAtIndex:section] name];
}

-(UITableViewRow*) tableView:tv cellForRowAtIndexPath:indexPath {
...
cell.text = [[[NSArray objectAtIndex:indexPath.section] objectAtIndex:indexPath.row] carName];
}

I hope this makes sense. I struggled with this a little myself when I first started.

Garrett
Aug 4, 2008, 04:26 PM
Well there are any number of ways to do it, but the first one that comes to mind is this:

Array of Sections (NSArray)
|_ Item 1 (NSDictionary)
|_ FirstSectionName (NSString) for key "SectionName"
|_ Array of Rows (NSArray) for key "RowsInSection"
|_ RowData1 (NSDictionary)
|_ Row1Date (NSString) for key "Date"
|_ Row1Mileage (NSString) for key "Mileage"
|_ Row1Gallons (NSString) for key "Gallons"
|_ RowData2 (NSDictionary)
|_ RowData3 (NSDictionary)
|_ Item 2 (NSDictionary)
|_ SecondSectionName (NSString) for key "SectionName"
|_ Array of Rows (NSArray) for key "RowsInSection"
|_ RowData1 (NSDictionary)
|_ RowData2 (NSDictionary)
|_ RowData3 (NSDictionary)
|_ RowData4 (NSDictionary)
|_ RowData5 (NSDictionary)

Of course, each dictionary at the bottom level of this structure would have the Date, Milegate, Gallons, and whatever other keys you need. I just left them out to avoid taking up tons of space.

Starting to get it now? Lay out the structure for your data based on how you need to use it, or alternatively however you think it would be most effectively accessed or stored.
Great overview, I am starting to get it. I just need to create an array with my objects? How can I set the keys for them? Is NSDictionary like a Array but able to define keys?

Taum
Aug 4, 2008, 04:50 PM
Is NSDictionary like a Array but able to define keys?
An NSDictionary is "like" an NSArray in that it's a data structure. Beyond that they don't have so much in common.

The concept of Dictionary is common in programming, but the name typically depends on the programming language used. Here is what wikipedia has to say :
Associative arrays have a variety of names. In Smalltalk, Objective-C, .NET, and Python they are called dictionaries; in Perl and Ruby they are called hashes; in C++ and Java they are called maps (see std::map and Map) and in Common Lisp and Windows PowerShell they are called hashtables (since both typically use this implementation). In PHP all arrays can be associative, except that the keys are limited to integers and strings and can only be a single level of subscripts.

If none of these mean anything to you, I'd also suggest some reading about basic data structures in programming. It will certainly help you a lot in understanding the examples.

Sbrocket
Aug 4, 2008, 04:57 PM
Great overview, I am starting to get it. I just need to create an array with my objects? How can I set the keys for them? Is NSDictionary like a Array but able to define keys?

Key-value coding is very central to large parts of Cocoa and CocoaTouch, so I would recommend that you read this conceptual article from Apple; its a good one (most of Apple's documentation is good, very thorough):
http://developer.apple.com/documentation/Cocoa/Conceptual/KeyValueCoding/KeyValueCoding.html

Each value in a dictionary has an associated key which you can access the value through. Conceptually, you can think of it as two matched and ordered arrays - one with keys, and one with objects.


Key Value
@"Date" @"August 4, 2008"
@"Mileage" @"5231"
@"Make" @"Honda"
@"Model" @"Accord"


Of course, you can store any object as a value (and also as a key, but the use is more limited) rather than just instances of NSString. Then you can access the values as follows.


[myDictionary valueForKey:@"Date"];
[myDictionary valueForKey:@"Mileage"];
etc...


You can construct the dictionary in one of two major ways: creating two NSArray instances, one for keys and one for values, and creating an NSDictionary from that; or you can use NSMutableDictionary.


// First method
NSArray *keys = [NSArray arrayWithObjects:@"Date",@"Mileage",@"Make",@"Model",nil];
NSArray *values = [NSArray arrayWithObjects:@"August 4, 2008",[NSNumber numberWithInt:5231],@"Honda",@"Accord",nil];
NSDictionary *dict = [[NSDictionary alloc] initWithObjects:values forKeys:keys];

// Second method
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
[dict setValue:@"August 4, 2008" forKey:@"Date"];
[dict setValue:[NSNumber numberWithInt:5231] forKey:@"Mileage"];
[dict setValue:@"Honda" forKey:@"Make"];
[dict setValue:@"Accord" forKey:@"Accord"];

Garrett
Aug 4, 2008, 05:34 PM
Key-value coding is very central to large parts of Cocoa and CocoaTouch, so I would recommend that you read this conceptual article from Apple; its a good one (most of Apple's documentation is good, very thorough):
http://developer.apple.com/documentation/Cocoa/Conceptual/KeyValueCoding/KeyValueCoding.html

Each value in a dictionary has an associated key which you can access the value through. Conceptually, you can think of it as two matched and ordered arrays - one with keys, and one with objects.


Key Value
@"Date" @"August 4, 2008"
@"Mileage" @"5231"
@"Make" @"Honda"
@"Model" @"Accord"


Of course, you can store any object as a value (and also as a key, but the use is more limited) rather than just instances of NSString. Then you can access the values as follows.


[myDictionary valueForKey:@"Date"];
[myDictionary valueForKey:@"Mileage"];
etc...


You can construct the dictionary in one of two major ways: creating two NSArray instances, one for keys and one for values, and creating an NSDictionary from that; or you can use NSMutableDictionary.


// First method
NSArray *keys = [NSArray arrayWithObjects:@"Date",@"Mileage",@"Make",@"Model",nil];
NSArray *values = [NSArray arrayWithObjects:@"August 4, 2008",[NSNumber numberWithInt:5231],@"Honda",@"Accord",nil];
NSDictionary *dict = [[NSDictionary alloc] initWithObjects:values forKeys:keys];

// Second method
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
[dict setValue:@"August 4, 2008" forKey:@"Date"];
[dict setValue:[NSNumber numberWithInt:5231] forKey:@"Mileage"];
[dict setValue:@"Honda" forKey:@"Make"];
[dict setValue:@"Accord" forKey:@"Accord"];


Thank you so much. I will make sure to give you credit on my app.

So to store that NSDictionary, I can just do [mainDelegate.carsArray insertObject:dict atIndex:0]; ? (Just a general look at it, it acts as an array or an object)