Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 01-22-2012, 03:51 PM   #1 (permalink)
generic twenty-something
 
jakerocheleau's Avatar
 
Join Date: Dec 2011
Location: Massachusetts
Age: 20
Posts: 54
jakerocheleau is on a distinguished road
Default Programmatically Build UITableView Sections & Populate Inner Cells

I have some code right now that can fill in a standard UITableView when set to 'Dynamic Prototypes'. I am loading these programmatically because I have a lot of rows and it gets confusing to detect which row was tapped. Here's the code I have so far:

PHP Code:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{
    return 
1;
}

- (
NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{
    return 
self.villagersListing.count;
}
- (
void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath 
{
    
    if (
indexPath.row 2)
    {
        [
cell setBackgroundColor:[UIColor colorWithRed:238.0f/255.0f green:244.0f/255.0f blue:244.0f/255.0f alpha:1.0]];
    }
    else [
cell setBackgroundColor:[UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:1.0]];
}

- (
UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
    
    static 
NSString *CellIdentifier = @"cell";
    
    
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
cell = [[UITableViewCell allocinitWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    
cell.accessoryType UITableViewCellAccessoryDisclosureIndicator;
    
cell.selectionStyle UITableViewCellSelectionStyleGray;
    
    
NSString *cellValue = [self.villagersListing objectAtIndex:indexPath.row];
    
cell.textLabel.text cellValue;
    
cell.textLabel.textColor = [UIColor colorWithRed:65.0f/255.0f green:65.0f/255.0f blue:65.0f/255.0f alpha:1.0];
    
    return 
cell;

This pulls data from an NSMutableArray called villagersListing and loads each string value into a UITableView. Towards the top I have numberOfSectionsInTableView set to 1 because I don't actually need any sections.

The problem is that I'm trying to list out a set of fruits/vegetables in a different 'Crops' table view. I need 4 sections - Spring, Summer, Fall, and Winter - and each contain a different array of crops. Currently I have this working through manually entering values in Storyboard but I need to find a way to display them through Objective-C.

I'm hoping somebody can help me translate my above code to use a set of Sections instead of just one long list of rows.

Please let me know if I can clarify anything... Here is what my crops table looks like in Storyboard right now:

Spring[TABLE VIEW SECTION]
  • Turnip
  • Potato
  • Cucumber
  • Cabbage
  • Strawberry

Summer[TABLE VIEW SECTION]
  • Onion
  • Tomato
  • Corn
  • Pineapple
  • Pumpkin
  • Pink Mist Flower

Fall[TABLE VIEW SECTION]
  • Carrot
  • Eggplant
  • Sweet Potato
  • Green Pepper
  • Breadfruit
  • Spinach
  • Magic Red Flower

Winter[TABLE VIEW SECTION]
  • White Snow Flower
__________________
I write, design apps, code Objective-C, and research my life away.
jakerocheleau is offline   Reply With Quote
Old 01-22-2012, 04:01 PM   #2 (permalink)
generic twenty-something
 
jakerocheleau's Avatar
 
Join Date: Dec 2011
Location: Massachusetts
Age: 20
Posts: 54
jakerocheleau is on a distinguished road
Default

I forgot to include a screenshot as well - I figure this may help explain things a bit clearer.

On the left is my current Crops table which is running through static cells I manually entered in the Storyboard view. On the right is my dynamically generated list of 'villagers' with all the custom cell styles.



also thanks in advance for any help I do appreciate it
__________________
I write, design apps, code Objective-C, and research my life away.
jakerocheleau is offline   Reply With Quote
Old 01-22-2012, 04:39 PM   #3 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

Well, if you need more than one section, then numberOfSections shouldn't be 1. I can't help with storyboard, but for general table view stuff, see the link in my signature.
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Old 01-22-2012, 04:49 PM   #4 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by jakerocheleau View Post
I forgot to include a screenshot as well - I figure this may help explain things a bit clearer.

On the left is my current Crops table which is running through static cells I manually entered in the Storyboard view. On the right is my dynamically generated list of 'villagers' with all the custom cell styles.



also thanks in advance for any help I do appreciate it


A sectioned tableview is basically an array of arrays. Each section is an array that contains an array of the elements in that section.

So, in cellForRowAtIndexPath, use the section number to select the outer array, and the row number to select the entry in that array.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 01-22-2012, 05:40 PM   #5 (permalink)
generic twenty-something
 
jakerocheleau's Avatar
 
Join Date: Dec 2011
Location: Massachusetts
Age: 20
Posts: 54
jakerocheleau is on a distinguished road
Default

Thanks I'll try to clarify a bit...

So I deleted all my Storyboard cells and changed the table to Dynamic Prototypes so I can control the cell values through Objective-C. Then I created a new UIView class for this view - it's called CropsListViewController and I added below the code I have so far.

I've set 4 sections each with the same array of vegetables. but the section headers don't actually show up, so it's just a repeating list of these 5 crops four different times.

PHP Code:
#import "CropsListViewController.h"

@implementation CropsListViewController
@synthesize springCropsList;

- (
void)viewDidLoad
{
    [
super viewDidLoad];
    
    
self.springCropsList = [[NSMutableArray allocinit];
    
    [
self.springCropsList addObject:@"Turnip"];
    [
self.springCropsList addObject:@"Potato"];
    [
self.springCropsList addObject:@"Cucumber"];
    [
self.springCropsList addObject:@"Cabbage"];
    [
self.springCropsList addObject:@"Strawberry"];
    
    
self.navigationItem.title = @"Crops";
}

- (
void)viewDidUnload
{
    [
super viewDidUnload];
    
// Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (
NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{
    return 
4;
}

- (
NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{
    return 
5;

So how would I go about setting each of the 4 different 'Header' values for the 4 different sections? Is this what you mean by creating an array of arrays, or an NSDictionary?
__________________
I write, design apps, code Objective-C, and research my life away.
jakerocheleau is offline   Reply With Quote
Old 01-22-2012, 05:42 PM   #6 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

That is explained in the table view link in my signature. Go read it. Not kidding.
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Old 01-23-2012, 10:50 AM   #7 (permalink)
generic twenty-something
 
jakerocheleau's Avatar
 
Join Date: Dec 2011
Location: Massachusetts
Age: 20
Posts: 54
jakerocheleau is on a distinguished road
Default

Quote:
Originally Posted by BrianSlick View Post
That is explained in the table view link in my signature. Go read it. Not kidding.
Dude that was perfect thanks so much. For anyone else with a similar issue here's the link to the tutorial i followed:

Clinging To Ideas: UITableView How-To: Part 3 - Multiple Sections
__________________
I write, design apps, code Objective-C, and research my life away.
jakerocheleau is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 388
10 members and 378 guests
7twenty7, Atatator, FrankWeller, glenn_sayers, iphonedevshani, MAMN84, mraalex, QuantumDoja, tim0504, VinceYuan
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,674
Threads: 94,122
Posts: 402,907
Top Poster: BrianSlick (7,990)
Welcome to our newest member, Atatator
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 05:47 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0