How to setup sub-arrays?

Discussion in 'iPhone/iPad Programming' started by Garrett, Aug 3, 2008.

  1. macrumors regular

    Joined:
    Apr 4, 2007
    #1
    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:
    [​IMG]

    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:
    Code:
    [mainDelegate.carsArray insertObject:newCar atIndex:[mainDelegate.carsArray count]];
    I assume I would be looking at something like:
    Code:
    [mainDelegate.carsArray insertObject:newEntry atIndex:[COLOR="Navy"][B][U]CAR-INDEX-HERE[/U][/B][/COLOR]];
    Thanks,
    Garrett
     
  2. macrumors 65816

    Joined:
    Jan 6, 2004
    #2
    Take a look at the TableViewSuite samples downloaded from Apple's Iphone dev site. It shows how to do exactly what you need.
     
  3. thread starter macrumors regular

    Joined:
    Apr 4, 2007
    #3
    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.
     
  4. thread starter macrumors regular

    Joined:
    Apr 4, 2007
    #4
    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:
     
  5. macrumors regular

    Joined:
    Mar 27, 2008
    #5
    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.
     
  6. thread starter macrumors regular

    Joined:
    Apr 4, 2007
    #6
    So I will have to do:

    Code:
    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?
     
  7. macrumors 65816

    Sbrocket

    Joined:
    Jun 3, 2007
    Location:
    /dev/null
    #7
    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.
     
  8. thread starter macrumors regular

    Joined:
    Apr 4, 2007
    #8
    I am not worried about the keys right now. I just have no reference on how to add rows to "sections".
     
  9. macrumors 65816

    Sbrocket

    Joined:
    Jun 3, 2007
    Location:
    /dev/null
    #9
    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.
     
  10. thread starter macrumors regular

    Joined:
    Apr 4, 2007
    #10
    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!
     
  11. macrumors 65816

    Sbrocket

    Joined:
    Jun 3, 2007
    Location:
    /dev/null
    #11
    Well there are any number of ways to do it, but the first one that comes to mind is this:

    Code:
    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.
     
  12. macrumors regular

    Joined:
    Mar 27, 2008
    #12
    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:

    Code:
    
    -(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.
    
    
     
  13. thread starter macrumors regular

    Joined:
    Apr 4, 2007
    #13
    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?
     
  14. macrumors member

    Joined:
    Jul 28, 2008
    #14
    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.
     
  15. macrumors 65816

    Sbrocket

    Joined:
    Jun 3, 2007
    Location:
    /dev/null
    #15
    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.

    Code:
    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.

    Code:
    [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.

    Code:
    // 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"];
    
     
  16. thread starter macrumors regular

    Joined:
    Apr 4, 2007
    #16
    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)
     

Share This Page