UITableViews: Pushing a detail view using NSDictionaries

Опубликовано: 16 Март 2026
на канале: MilmersXcode
13,664
68

In this UITableView tutorial I teach you how to push a 'Detail View" using only 1 view to display different data depending on what row is clicked. This is done using NSDictionaries to store the data and we use keys to get the data from them.

Make new detailView - PetsDetailViewController.

IBOutlet UIImageView *petImage;
IBOutlet UILabel *petLabel;
NSString *petImageString;
NSString *petLabelString;
}

@property (nonatomic, retain) NSString *petLabelString;
@property (nonatomic, retain) NSString *petImageString;

In the .m
@synthesize petImageString,petLabelString;

viewDidLoad
petImage.image = [UIImage imageNamed:petImageString];
petLabel.text = petLabelString;


PetsTableView

In the .h
-(void) makeData;
In the .m
#import "PetsDetailViewController.h"

//In the viewDidLoad delete old array and add:
[self makeData];

-(void) makeData {

dogArray = [[NSMutableArray alloc] init];
catArray = [[NSMutableArray alloc] init];
snakeArray = [[NSMutableArray alloc] init];

//Dogs
[dogArray addObject:[[NSMutableDictionary alloc]
initWithObjectsAndKeys:@"Dog 1", @"name" ,
@"Dog.png", @"image" ,
@"He said it. Smile :D", @"description" ,nil]];
[dogArray addObject:[[NSMutableDictionary alloc]
initWithObjectsAndKeys:@"Dog 2", @"name" ,
@"Dog2.png", @"image" ,
@"He's talking to you.", @"description" ,nil]];
//Cats
[catArray addObject:[[NSMutableDictionary alloc]
initWithObjectsAndKeys:@"Cat 1", @"name" ,
@"Cat.png", @"image" ,
@"Helmet Cat", @"description" ,nil]];

//Snakes
[snakeArray addObject:[[NSMutableDictionary alloc]
initWithObjectsAndKeys:@"Snake 1", @"name" ,
@"Snake.png", @"image" ,
@"Scaly Snake", @"description" ,nil]];
}

//Go to cellForRowAtIndexPath:

//Change slightly to - cell.textLabel.text = [[dogArray objectAtIndex:indexPath.row] objectForKey:@"name"];

//Go to didSelectRowAtIndexPath:
//Type: PetsDetailViewController *petsDetail = [[PetsDetailViewController alloc] initWithNibName:@"PetsDetailViewController" bundle:nil];
//Then do the if statements to work out which row was pressed.
if (petsInt == 0)
{
petsDetail.petImageString = [[NSString alloc] initWithString:[[dogArray objectAtIndex:indexPath.row] objectForKey:@"image"]];
petsDetail.petLabelString = [[NSString alloc] initWithString:[[dogArray objectAtIndex:indexPath.row] objectForKey:@"description"]];
petsDetail.title = [[dogArray objectAtIndex:indexPath.row] objectForKey:@"name"];
}
//Just copy and past 2 more times for the Cat and Snake arrays (I couldn't add them otherwise the description would be too long).

//Push and release.
[self.navigationController pushViewController:petsDetail animated:YES];
[petsDetail release];

IB
Drag in pics (If haven't already) and connect label and image view.

Next Video:    • iPhone Programming - UITableViews: Making ...  
UITableView Playlist: http://www.youtube.com/playlist?p=PLD...
Previous Video:    • iPhone Programming - UITableViews: Pushing...  

Twitter:   / failcakeapps  
Apple Developer Center: http://developer.apple.com/devcenter/...
Website: http://failcake.webs.com/
Channel:    / milmersxcode