I used to use the view controller as my data model also.
Your Plist should be structured like so.
- root (Array)
- Album one (NSDictionary)
- Album Image (NSString)
- Album Label (NSString)
- Album Tracklist (NSString)
- Album two (NSDictionary)
- Album Image (NSString)
- Album Label (NSString)
- Album Tracklist (NSString)
- Album three (NSDictionary)
- Album Image (NSString)
- Album Label (NSString)
- Album Tracklist (NSString)
- Album four (NSDictionary)
- Album Image (NSString)
- Album Label (NSString)
- Album Tracklist (NSString)
- Album one (NSDictionary)
The Code:
CollectionViewController.h : Empty it.
CollectionViewController.m:
#import "CollectionViewController.h"
#import "Cell.h"
#import "ImageDetailViewController.h"
@interface CollectionViewController ()
@property (nonatomic, strong) NSArray *albums;
@end
@implementation CollectionViewController
- (void)viewDidLoad
{
[super viewDidLoad];
//You dont want this.
/*self.albumImages = @[@"1998_Yellow.png",@"1998_WoM",@"2000_PoTY",@"2001_SFA",@"2003_MFZB",@"2004_WoMFZB",@"2006_BttW",@"2008_NtnA",@"2008_Phoenix",@"2009_PantyRaid",@"2011_GetNice",@"2013_CyF"];
self. albumDescriptions = @[@"Yellow [1998]",@"Waste of Mind [1998]",@"Playmate of the Year [2000]",@"Stupid Fat Americans - EP [2001]",@"MFZB [2003]",@"Waste of MFZB [2004]",@"Broadcast to the World [2006]",@"Not the New Album - EP [2008]",@"Phoenix [2008]",@"Panty Raid [2009]",@"Get Nice! [2011]",@"Call Your Friends [2013]" ];*/
//Do this if you are using a plist
//Makes everything thread happy (maximal responsiveness)
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); //Get path of application
NSString *documentsDirectory = [paths objectAtIndex:0]; //Get documents path
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"data.plist"]; //Get path to dictionary
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath: path]) //Check if file exists
{
NSString *bundle = [[NSBundle mainBundle] pathForResource:@"data" ofType:@"plist"]; //Locate file in bundle
[fileManager copyItemAtPath:bundle toPath: path error:&error]; //Copy file over
}
self.albums = [[NSArray alloc] initWithContentsOfFile: path]; //Initalize dictionary from plist
dispatch_async(dispatch_get_main_queue(), ^{
[self.collectionView reloadData]; //Reload your data
});
});
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return [self.albums count];
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
Cell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MY_CELL" forIndexPath:indexPath];
UIImage *albumImage = [[UIImage alloc] init];
albumImage = [UIImage imageNamed:[[self.albums objectAtIndex:indexPath.row] objectForKey:@"Album Image"]];
cell.imageView.image = albumImage;
return cell;
NSLog(@"Cellule cliquée");
}
#pragma mark - Prepare for Segue
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
UICollectionViewCell *cell = (UICollectionViewCell *)sender;
NSIndexPath *indexPath = [self.collectionView indexPathForCell:cell];
NSLog(@"Segue versDetails");
ImageDetailViewController *imageDetailViewController = (ImageDetailViewController *)segue.destinationViewController;
imageDetailViewController.album = [self.albums objectAtIndex:indexPath.row];
}
- (void)viewWillAppear:(BOOL)animated
{
[self.navigationController setNavigationBarHidden:YES animated:animated];
[super viewWillAppear:animated];
}
- (void)viewDidDisappear: (BOOL)animated
{
[self.navigationController setNavigationBarHidden:NO animated:animated];
[super viewDidDisappear:animated];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
ImageDetailViewController.h:
@interface ImageDetailViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
@property (strong, nonatomic) IBOutlet UIImageView *albumImageView;
@property (strong, nonatomic) IBOutlet UILabel *albumDetailLabel;
/*@property (strong, nonatomic) UIImage *albumImage;
@property (strong, nonatomic) NSString *albumLabelText;*/
@property (nonatomic, strong) NSDictionary *album;
/*@property (strong,nonatomic) NSArray *tracklist_Y;
@property (strong,nonatomic) NSArray *tracklist_WoM;
@property (strong,nonatomic) NSArray *tracklist_PotY;
@property (strong,nonatomic) NSArray *tracklist_SFA;
@property (strong,nonatomic) NSArray *tracklist_MFZB;
@property (strong,nonatomic) NSArray *tracklist_WoMFZB;
@property (strong,nonatomic) NSArray *tracklist_BttW;
@property (strong,nonatomic) NSArray *tracklist_NtnA;
@property (strong,nonatomic) NSArray *tracklist_Ph;
@property (strong,nonatomic) NSArray *tracklist_PR;
@property (strong,nonatomic) NSArray *tracklist_GN;
@property (strong,nonatomic) NSArray *tracklist_CyF;*/
@end
ImageDetailViewController.m
#import "ImageDetailViewController.h"
@interface ImageDetailViewController ()
@end
@implementation ImageDetailViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [[self.album objectForKey:@"Album Tracklist"] count];
//return [self.tracklist_Y count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//Liste des chansons (Tracklist)
//http://blog.safaribooksonline.com/2013/02/26/objective-c-how-to-populate-data-in-uitableviewcontroller/
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TrackCell"];
if(cell == nil){
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"TrackCell"];
}
cell.textLabel.text = [[self.album objectForKey:@"Album Tracklist"] objectAtIndex:indexPath.row];
cell.textLabel.textColor = [UIColor whiteColor];
return cell;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationController.navigationBar.hidden = NO;
self.albumDetailLabel = [self.album objectForKey:@"Album Label"];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
So basically here, you have an array of your albums, and you are extracting the image, label, and track list when you do the object for key thing. I tried my best to make your code plist friendly, but it may not be perfect...