Alert icon
We're changing our privacy policy. This stuff matters.  Learn more  Dismiss

iPhone Programming - UITableViews: Making Sections

Loading...

Sign in or sign up now!
Alert icon
Upgrade to the latest Flash Player for improved playback performance. Upgrade now or more info.
4,279
Loading...
Alert icon
Sign in or sign up now!
Alert icon

Uploaded by on Jul 22, 2011

In this UITableView tutorial I teach you how to create sections in a UITableView and the 2 different styles they can be.

Main Code Used:

PetsTableView
In the .h
NSMutableArray *dog2Array;
NSMutableArray *sectionsArray;
NSMutableArray *data;
In the .m

-(void) makeData {
data = [[NSMutableArray alloc] init];
dog2Array = [[NSMutableArray alloc] init];
sectionsArray = [[NSMutableArray alloc] init];

if (petsInt == 0) { [sectionsArray addObject:@"DogSection 1"]; [sectionsArray addObject:@"DogSection 2"]; } if (petsInt == 1) [sectionsArray addObject:@"CatSection"]; if (petsInt == 2) [sectionsArray addObject:@"SnakeSection"];

//Just need to change "dogArray" to "dog2Array".
[dog2Array addObject:[[NSMutableDictionary alloc] initWithObjectsAndKeys:@"Dog 2", @"name" , @"Dog2.png", @"image" , @"He's talking to you.", @"description" ,nil]];

if (petsInt == 0) { [data addObject:dogArray]; [data addObject:dog2Array]; } if (petsInt == 1) [data addObject:catArray]; if (petsInt == 2) [data addObject:snakeArray];
}

//Go to numberOfSectionsInTableView:

return [sectionsArray count];

//Go to numberOfRowsInSection:

return [[data objectAtIndex:section] count];

//Add:

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
return [sectionsArray objectAtIndex:section];
}

//Go to cellForRowAtIndexPath:
cell.textLabel.text = [[[data objectAtIndex:indexPath.section] objectAtIndex:indexPath.row] objectForKey:@"name"];
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];

//Go to didSelectRowAtIndexPath:
petsDetail.petImageString = [[NSString alloc] initWithString:[[[data objectAtIndex:indexPath.section] objectAtIndex:indexPath.row] objectForKey:@"image"]]; petsDetail.petLabelString = [[NSString alloc] initWithString:[[[data objectAtIndex:indexPath.section] objectAtIndex:indexPath.row] objectForKey:@"description"]]; petsDetail.title = [[[data objectAtIndex:indexPath.section] objectAtIndex:indexPath.row] objectForKey:@"name"];

IB
You can change the style of the sections under style in the Attributes tab when selecting the UITV.

Next Video: http://www.youtube.com/watch?v=oYKX3l9zCGM
UITableView Playlist: http://www.youtube.com/playlist?p=PLDE7D5FFAA97747AE&feature=mh_lolz
Previous Video: http://www.youtube.com/watch?v=99Ssk1-HUq4

Twitter: http://twitter.com/failcakeapps
Apple Developer Center: http://developer.apple.com/devcenter/ios/index.action
Website: http://failcake.webs.com/
Channel: http://youtube.com/milmersxcode

  • likes, 1 dislikes

Link to this comment:

Share to:

Uploader Comments (MilmersXcode)

  • how can you use the nsmutable array to display random images of type UIButton?

    how can you check if user touched the right image (game based)?

  • @acesamieh Image of type UIButton? Do you mean buttons with images? Check my new tutorial on creating custom methods. It teaches you how to create numerous buttons with the call of one method.

    If this doesn't help, PM me with more detail.

    Milmer.

  • Im getting this error, please help:

    *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSMutableArray objectAtIndex:]: index 2 beyond bounds [0 .. 0]'

    I've scoured the web, and can't find help. It happens at the "return [[data objectAtIndex:section] count];" under the numberOfRowsInSection NSInteger.

    Again, please help :)

  • @MysteriousKiwi This tends to happen when you add an "empty" or "nil" array to the dataArray. Try checking your arrays by logging them to see if they contain the data.

  • Wow awesome vid. I have been getting so confused by the dev apple code, this definately clears a lot of that up. Thanks a lot for the tut.

  • @iamdjalkaline Thanks. I know it's kind of hard to get to grasps with Apple Code but when you get your head around it it can be really helpful.

see all

All Comments (10)

Sign In or Sign Up now to post a comment!
  • @MilmersXcode Ok, thankyou very much :)

  • @stickosan Thats a custom API apple have made for that table view. I would in no way have a clue about starting to make that. Maybe ask on Apple Dev Forums or webistes like stackoverflow. Sorry I couldn't help much.

  • @MysteriousKiwi No, it's fine. At least I know people are interested :D

  • Nice :D

    You must be bored of me commenting on every vid :P

Loading...

Alert icon
0 / 00Unsaved Playlist Return to active list
    1. Your queue is empty. Add videos to your queue using this button:
      or sign in to load a different list.
    Loading...Loading...Saving...
    • Clear all videos from this list
    • Learn more