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
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 2 months ago
@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.
MilmersXcode 2 months ago
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 4 months ago
@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.
MilmersXcode 4 months ago
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 5 months ago
@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.
MilmersXcode 5 months ago