View Full Version : One Array, Multiple Sections?
Oct 13, 2009, 11:29 PM
I'm getting hung up on this part of my project.
I've tried a few things, nothing is giving me the result I want.
I have a UITableView populated with One Array, has about 30 items.
Now, I want 3 Sections, but I can't figure out how to have say:
items 1-10 in section 1, 11-20 in section 2, and 21-30 in section 3.
Everything I try gives me duplicates in each section.
Any Ideas for me?
Oct 14, 2009, 12:32 AM
It's an array of arrays of dictionaries. The dictionaries represent each row. The top level array holds the arrays for each section.
So in your case you have a top level array with three members. Each member is an array. Each of the section arrays holds ten dictionaries.
Oct 14, 2009, 01:04 PM
Oct 14, 2009, 01:45 PM
OK, here's some code from one of my apps. This code builds the data for the first section. The code for subsequent sections is similar but it accesses different data for the label and title or whatever the views are in the rows for those sections. In my case the data comes from a database and is stored in a custom class.
As should be obvious once you have built up this array of arrays of dictionaries the code for rowsInSection is simple, getting the data for a particular section/row is very simple.
// The SectionsList contains arrays, one array for each section.
// Each section array contains NSDictionaries, one dictionary for each row
// The row dictionaries contain the data for each row, label, value,
// title. The title is the title for the section and is present
// only in the row 0 dictionary.
// Building this array of arrays of dictionaries when the view
// appears makes the code for the tableview callbacks much simpler.
mSectionsList = [[NSMutableArray alloc] initWithCapacity:10];
// Build header section
NSMutableArray* sectionArray = [[NSMutableArray alloc] init];
NSString* label = nil;
NSString* text = nil;
NSString* title = nil;
// Add Name and Event
// No title for header section
for (NSUInteger i = 0; i < 2; i++)
label = [self labelForHeadingSection:i];
text = [self textForHeadingSection:i];
rowDictionary = [[NSDictionary alloc] initWithObjectsAndKeys:label, kLabelKey, text, kTextKey, nil];
// build additional section arrays here ...