One Array, Multiple Sections?

Discussion in 'iPhone/iPad Programming' started by Rappa819, Oct 13, 2009.

  1. macrumors newbie

    Joined:
    Feb 28, 2008
    #1
    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?
     
  2. macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #2
    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.

    Simple.
     
  3. thread starter macrumors newbie

    Joined:
    Feb 28, 2008
    #3
    Example?
     
  4. macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #4
    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.

    Code:
    - (void)viewDidLoad 
    {
    	// 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];
    	NSDictionary*	rowDictionary;
    	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];
    		[sectionArray addObject:rowDictionary];
    		[rowDictionary release];
    	}
    	
    	[mSectionsList addObject:sectionArray];
    	[sectionArray release];
    
    	// build additional section arrays here ...
    
    	[self.tableView reloadData];
    }
    
     

Share This Page