memory issue nsobject and nsmutablearray UICollectionView - ios
I added a simple collection view and i add items fetched from my backend , when I add all the objects to a nsmutablearray it takes less memory but when I add the fetched objects to a NSObject while scrolling the collection view it takes more memory .
here is a sample code
myObjectModel.h
#import <Foundation/Foundation.h>
#interface myObjectModel: NSObject
#property (nonatomic, strong) NSString *objectTitle;
#property (nonatomic, strong) NSString *objectDesc;
#property (nonatomic, strong) NSString *objectImgURL;
- (id)initWithObjectTitle:(NSString *)dTitle
objectImgURL:(NSString *)dObjectImgURL
objectDesc:(NSString *)dObjectDesc;
#end
myObjectModel.m
#import "myObjectModel.h"
#implementation myObjectModel
- (id)initWithObjectTitle:(NSString *)dTitle
objectImgURL:(NSString *)dObjectImgURL
objectDesc:(NSString *)dObjectDesc {
self = [super init];
if (self) {
self.objectTitle = dTitle;
self.objectImgURL = dObjectImgURL;
self.objectDesc = dObjectDesc;
}
return self;
}
#end;
then I imported this header to my UIViewController which has a collectionview while fetching all the objects i add this method to add those objects and then save them into a nsmutablearray
BackendlessDataQuery *query = [BackendlessDataQuery query];
query.queryOptions.pageSize = #(100); //set page size
[[backendless.persistenceService of:[Channels class]] find:query response:^(BackendlessCollection *backendlessObjects) {
NSArray *currentPage = [backendlessObjects getCurrentPage];
NSMutableArray *arrData = [[NSMutableArray alloc]init];
for (Objects *objects in currentPage) {
#autoreleasepool {
myObjectModel *dataItem = [[myObjectModel alloc]initWithObjectTitle:backendlessObjects.title
objectImgURL:backendlessObjects.image
objectDesc:backendlessObjects.Desc;
[arrData addObject:dataItem];
}
//Test is a nsarray added in the header file
self.test = arrData;
[self.collectionView reloadData];
} error:^(Fault *fault) {
}];
here is what I add to my collection data source
#pragma mark - UICollectionViewDatasource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView; {
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return [test count];
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
myCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"data" forIndexPath:indexPath];
myObjectModel *dataItem = (myObjectModel*)[test objectAtIndex:indexPath.row];
// cell.objectTitle.text = dataItem.objectTitle;
[cell.channelImg sd_setImageWithURL:[NSURL URLWithString:dataItem.objectImgURL] placeholderImage:[UIImage imageNamed:#"NoImage.png"]];
return cell;
}
when I run this code all my objects will appear in the collectionview but while scrolling the memory keeps increasing.
There is no reason to increase memory by using test Array the reason is by using this
[cell.channelImg sd_setImageWithURL:[NSURL URLWithString:dataItem.objectImgURL] placeholderImage:[UIImage imageNamed:#"NoImage.png"]];
this will Asynchronously download image from URL.if you comment above line then it may not increase memory.
Related
Programatically creating the custom UICollectionViewCell and setting the properties
I've been trying to configure a custom UICollectionViewCell that is supposed to present a UIView consisting of a few subviews generated with a custom method. The problem is that when I try to pass the needed data from -cellForItemAtIndexPath to the cell's properties (NSStrings) the app crashes and the data doesn't seem to be passed at all, because NSLog returns (null) for all of the properties. I thought that the problem was with the properties not being initialized, but when I initialized them in -initWithFrame, they didn't return any values. I tried to use the -initWithCoder and -awakeFromNib and it didn't work. I also tried to utilize a custom method that would set the properties for me. While it seems to be working, I can't seem to find a way to add the generated view to the cell's view, as using [self.contentView addSubview:...] seems to be adding every generated view to every cell in the Collection View. If you guys could help me out, I would be much grateful - I've spent my entire day on this, and I just can't get it to work. Perhaps I should do it differently? My custom collection view cell's .h file: #import "SHDesignChicago.h" #interface SHDesignChicagoCollectionViewCell : UICollectionViewCell #property (strong, nonatomic) NSString *name; #property (strong, nonatomic) NSString *date; #property (strong, nonatomic) NSString *going; #property (strong, nonatomic) PFFile *background; #property (strong, nonatomic) UIView *generatedDesign; - (void)setCellWithEventName:(NSString *)name eventDate:(NSString *)date eventGoing:(NSString *)going eventBackground:(PFFile *)background; #end And my custom collection view cell's .m file: #import "SHDesignChicagoCollectionViewCell.h" #implementation SHDesignChicagoCollectionViewCell - (void)setCellWithEventName:(NSString *)name eventDate:(NSString *)date eventGoing:(NSString *)going eventBackground:(PFFile *)background { UIImage *backgroundImage = [[UIImage alloc] initWithData:[background getData]]; NSLog(#"Event details:\nName: %#\nDate: %#\nGoing: %#\nBackground: %#", name, date, going, background); self.generatedDesign = [SHDesignChicago designWithName:name date:date going:going background:backgroundImage frame:self.frame]; } - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { //self.autoresizesSubviews = YES; __block UIImage *backgroundImage; [self.background getDataInBackgroundWithBlock:^(NSData *data, NSError *error) { if (!error) backgroundImage = [[UIImage alloc] initWithData:data]; }]; NSLog(#"Event details:\nName: %#\nDate: %#\nGoing: %#\nBackground: %#", self.name, self.date, self.going, self.background); if (self.name != nil) { UIView *generatedDesign = [SHDesignChicago designWithName:self.name date:self.date going:self.going background:backgroundImage frame:self.frame]; [self addSubview:generatedDesign]; } } return self; } #end Also the cellForItemAtIndexPath: - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { PFObject *event = [self.events objectAtIndex:indexPath.row]; NSString *name = event[#"eventName"]; NSDate *date = event[#"eventDate"]; PFFile *background = event[#"coverPhoto"]; if ([event[#"designId"] isEqual: #(1)]) { // SHDesignChicago SHDesignChicagoCollectionViewCell *chicagoCell = (SHDesignChicagoCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:#"chicago" forIndexPath:indexPath]; /* chicagoCell.name = #"TEST"; chicagoCell.date = #"TODAY, 5 PM"; chicagoCell.going = #"NATALIA KAWECKA AND 35 OTHERS ARE GOING"; chicagoCell.background = background; */ [chicagoCell setCellWithEventName:name eventDate:#"TODAY, 5 PM" eventGoing:#"NATALIA M AND 35 OTHERS ARE GOING" eventBackground:background]; return chicagoCell; } UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"Cell" forIndexPath:indexPath]; return cell; }
How do I use a Plist file with a UITableView / UICollectionView?
I'm creating a basic music app - the albums of a band display in a UICollectionView first, then when I click on an album, I get pushed to the detail view, which displays information about the selected album, along with the album tracklist. The point of the app, is to be able to click on a tracklist cell, and the song will play. I have these massive Arrays that contain the full tracklist of all the albums, not cool, too big. I was told that I could use a Plist file. However, I wonder how I can put all the information in one plist file, since I have several albums, and therefore several tracklists. Should I create one plist file listing all my albums? Then where should I insert the tracklist next? Can you point me to a solution? Here's a screenshot of my storyboard: Click here to view screenshot full screen. I am a beginner, and would like to optimize my code. I can't get the song in the cell to play yet. CollectionViewController.h: #import <UIKit/UIKit.h> #interface CollectionViewController : UICollectionViewController #property (strong, nonatomic) NSArray *albumImages; #property (strong,nonatomic) NSArray *albumDescriptions; #end CollectionViewController.m: #import "CollectionViewController.h" #import "Cell.h" #import "ImageDetailViewController.h" #interface CollectionViewController () #end #implementation CollectionViewController - (void)viewDidLoad { [super viewDidLoad]; 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]" ]; } - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return self.albumImages.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.albumImages objectAtIndex:indexPath.row]]; 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.albumImage = [UIImage imageNamed:[self.albumImages objectAtIndex:indexPath.row]]; imageDetailViewController.albumLabelText = [self. albumDescriptions 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 (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" #import "animationY.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 10; //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/ self.tracklist_Y = [[NSArray alloc]initWithObjects:#"1. Check",#"2. All I Need",#"3. Swing",#"4. Walk Away",#"5. Bootylicious Vinyl",#"6. Hate",#"7. Mindtrip",#"8. Chrome",#"9. Jag Off",#"10. Song 10",nil]; self.tracklist_WoM = [[NSArray alloc]initWithObjects:#"1. Check",#"2. Get Back",#"3. The Real Me",#"4. Someday",#"5. Waste of Mind",#"6. Feel This Way",#"7. Walk Away",#"8. Big Shot",#"9. Swing",#"10. Jag Off",#"11. Time",#"12. Move On",#"13. Flay Daze",#"14. Bootylicious Vinyl",nil]; self.tracklist_PotY = [[NSArray alloc]initWithObjects:#"1. I Am",#"2. Playmate of the Year",#"3. Now or Never",#"4. Wasted",#"5. I'm Money",#"6. Go",#"7. What's Goin' On?",#"8. Subtract You",#"9. The Hell that is my Life",#"10. E Generation",#"11. Livin' Libido Loco",#"12. In My Room",nil]; self.tracklist_SFA = [[NSArray alloc]initWithObjects:#"1. Wasted",#"2. Chrome [Demo Version]",#"3. Swing [Demo Version]",#"4. Deck the Halls (I Hate Christmas) W/Intro",#"5. Jag Off [Live]",#"6. Someday [Live]",#"7. Get Back [Live]",nil]; self.tracklist_MFZB = [[NSArray alloc]initWithObjects:#"1. Rescue Me",#"2. Over the Edge",#"3. Strength",#"4. Hello Tomorrow",#"5. The Set-Up",#"6. Blur",#"7. House is Not my Home",#"8. Into You",#"9. Alone",#"10. Expectations",#"11. Falling Apart",#"12. Let it Ride",#"13. Type A",#"14. Runaway",#"15. Dear You (Far Away)",#"16. The Fear",#"17. Surrender [*]",#"18. Good Things [*]",#"19. Dissatisfied [*]",nil]; self.tracklist_WoMFZB = [[NSArray alloc]initWithObjects:#"1. Are You For Real?",#"2. Let Me Go",#"3. One Less Headache]",#"4. Burn the School Down",#"5. Lightning Rod",#"6. Blindside",#"7. Veils and Visions",#"8. One Shot",#"9. Timing is Everything",#"10. Wannabe",nil]; self.tracklist_BttW = [[NSArray alloc]initWithObjects:#"1. Broadcast to the World",#"2. Rated \"U\" for Ugly",#"3. Anthem",#"4. Enemy",#"5. Back to Normal",#"6. Postcards from Hell",#"7. Karma Flavored Whisky",#"8. Here's to You",#"9. Wake me Up",#"10. Lobotomy for Dummies",#"11. The Walking Dead",#"12. Your New Boyfriend Wears Girl Pants",#"13. Riot Girl [*]",#"14. Down in Flames [*]",#"15. Get on the Bus [*]",#"16. Hit it Again [*]",nil]; self.tracklist_NtnA = [[NSArray alloc]initWithObjects:#"1. Mental Health",#"2. Photographs",#"3. Politics",#"4. The Art of Breaking Up",#"5. We're Not a Cover Band, We're a Tribute Band",nil]; self.tracklist_Ph = [[NSArray alloc]initWithObjects:#"1. HMP",#"2. Hell Yeah!",#"3. Just the Tip",#"4. Mental Health",#"5. The Juggernauts",#"6. Death by Disco",#"7. Be Careful What You Wish For",#"8. Morse Code for Suckers",#"9. Ignite",#"10. Mike Dexter is a God, Mike Dexter is a Role Model, Mike Dexter is an A**hole",#"11. The Junkie and the Halo",#"12. Brixton",#"13. Hit the Ground",#"14. Two Wrongs don't make a Right, but Three Rights make a Left",#"15. All for None and None for All",#"16. Sorry, but Your Friends are Hot",#"17. The Art of Breaking Up [*]",#"18. We're Not a Cover Band, We're a Tribute Band [*]",nil]; self.tracklist_PR = [[NSArray alloc]initWithObjects:#"1. Survivor",#"2. Girls Just Want to Have Fun",#"3. Underneath it All",#"4. Trouble",#"5. London Bridge",#"6. Beautiful",#"7. Girlfriend",#"8. The Sweet Escape",#"9. Jenny from the Block",#"10. Rehab",#"11. Spice Up Your Life",#"12. Oops!... I Did it Again",#"13. Get the Party Started",#"14. Mickey",#"15. All I Want for Christmas is You [*]",#"16. Who Let the Dogs Out [*]",nil]; self.tracklist_GN = [[NSArray alloc]initWithObjects:#"1. Blackout",#"2. Nothing to Lose",#"3. She Don't Wanna Rock",#"4. Ricky Bobby",#"5. Get Nice!",#"6. The Joke's on You",#"7. Nudist Priest",#"8. Galileo Was Wrong",#"9. Truck Stops and Tail Lights",#"10. I'm Definitely Not Gonna Miss You",#"11. Too Bored to Bleed",#"12. Kiss your Ass Goodbye",#"13. This is Gonna Hurt You Way More than it's Gonna Hurt Me",#"14. Demon Days",#"15. Light Up the Sky [*]",#"16. A Freak Gasoline Fight Accident [*]",nil]; self.tracklist_CyF = [[NSArray alloc]initWithObjects:#"1. Sirens",#"2. I'm Just Here for the Free Beer",#"3. With Friends Like These, Who Needs Herpes?",#"4. Call Your Friends",#"5. Murder on the Airwaves",#"6. Public Enemy Number One",#"7. Born to Lose",#"8. Stick Em Up Kid!",#"9. Automatic",#"10. Nerd Armor",#"11. Panic in the Streets",#"12. Don't Believe the Hype",#"13. Until the Sun Comes Up",#"14. Last Call",#"15. Sex, Lies & Audiotape [*]",#"16. Battle of the Bullshit [*]",#"17. Ready Steady Go [*]",nil]; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"TrackCell"]; if(cell == nil){ cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"TrackCell"]; } // Vérification de quel album a été cliqué; // Il semble qu'il n'y ait pas moyen d'utiliser un Switch avec un NSString, selon StackOverflow : http://goo.gl/dpylqF if ([_albumDetailLabel.text isEqualToString:#"Yellow [1998]"]) { animationY * jouerAnimationY = [[animationY alloc]initWithFrame:CGRectMake(85, -20, 600, 600) prefixNom:#"yellowAnim" nbImages:120]; [self.view addSubview: jouerAnimationY ]; cell.textLabel.text = [self.tracklist_Y objectAtIndex:indexPath.row]; cell.textLabel.textColor=[UIColor whiteColor]; return cell; /*cell.textLabel.text = #"Yellow"; return cell;*/ } else if ([_albumDetailLabel.text isEqualToString:#"Waste of Mind [1998]"]) { cell.textLabel.text = [self.tracklist_WoM objectAtIndex:indexPath.row]; cell.textLabel.textColor=[UIColor whiteColor]; return cell; /*cell.textLabel.text = #"Waste of Mind"; return cell;*/ } else if ([_albumDetailLabel.text isEqualToString:#"Playmate of the Year [2000]"]) { cell.textLabel.text = [self.tracklist_PotY objectAtIndex:indexPath.row]; cell.textLabel.textColor=[UIColor whiteColor]; return cell; /*cell.textLabel.text = #"Playmate of the Year"; return cell;*/ } else if ([_albumDetailLabel.text isEqualToString:#"Stupid Fat Americans - EP [2001]"]) { cell.textLabel.text = [self.tracklist_SFA objectAtIndex:indexPath.row]; cell.textLabel.textColor=[UIColor whiteColor]; return cell; /*cell.textLabel.text = #"Stupid Fat Americans"; return cell;*/ } else if ([_albumDetailLabel.text isEqualToString:#"MFZB [2003]"]) { cell.textLabel.text = [self.tracklist_MFZB objectAtIndex:indexPath.row]; cell.textLabel.textColor=[UIColor whiteColor]; return cell; /*cell.textLabel.text = #"MFZB"; return cell;*/ } else if ([_albumDetailLabel.text isEqualToString:#"Waste of MFZB [2004]"]) { cell.textLabel.text = [self.tracklist_WoMFZB objectAtIndex:indexPath.row]; cell.textLabel.textColor=[UIColor whiteColor]; return cell; /*cell.textLabel.text = #"Waste of MFZB"; return cell;*/ } else if ([_albumDetailLabel.text isEqualToString:#"Broadcast to the World [2006]"]) { cell.textLabel.text = [self.tracklist_BttW objectAtIndex:indexPath.row]; cell.textLabel.textColor=[UIColor whiteColor]; return cell; /*cell.textLabel.text = #"Broadcast to the World"; return cell;*/ } else if ([_albumDetailLabel.text isEqualToString:#"Not the New Album - EP [2008]"]) { cell.textLabel.text = [self.tracklist_NtnA objectAtIndex:indexPath.row]; cell.textLabel.textColor=[UIColor whiteColor]; return cell; /*cell.textLabel.text = #"Not the New Album"; return cell;*/ } else if ([_albumDetailLabel.text isEqualToString:#"Phoenix [2008]"]) { cell.textLabel.text = [self.tracklist_Ph objectAtIndex:indexPath.row]; cell.textLabel.textColor=[UIColor whiteColor]; return cell; /*cell.textLabel.text = #"Phoenix"; return cell;*/ } else if ([_albumDetailLabel.text isEqualToString:#"Panty Raid [2009]"]) { cell.textLabel.text = [self.tracklist_PR objectAtIndex:indexPath.row]; cell.textLabel.textColor=[UIColor whiteColor]; return cell; /*cell.textLabel.text = #"Panty Raid"; return cell;*/ } else if ([_albumDetailLabel.text isEqualToString:#"Get Nice! [2011]"]) { cell.textLabel.text = [self.tracklist_GN objectAtIndex:indexPath.row]; cell.textLabel.textColor=[UIColor whiteColor]; return cell; /*cell.textLabel.text = #"Get Nice!"; return cell;*/ } else if ([_albumDetailLabel.text isEqualToString:#"Call Your Friends [2013]"]) { cell.textLabel.text = [self.tracklist_CyF objectAtIndex:indexPath.row]; cell.textLabel.textColor=[UIColor whiteColor]; return cell; /*cell.textLabel.text = #"Call Your Friends"; return cell;*/ } } - (void)viewDidLoad { [super viewDidLoad]; self.navigationController.navigationBar.hidden = NO; self.albumImageView.image = self.albumImage; self.albumDetailLabel.text = self.albumLabelText; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } #end
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) 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...
Nothing apear in UIcollectionView Controller
I have UIcollectionView in my first view of application after Uinavigationviewcontroller just in storyboard just like this : this is my RootViewController.h #import <UIKit/UIKit.h> #interface RootViewController : UICollectionViewController<UICollectionViewDelegate,UICollectionViewDataSource> #property (nonatomic, strong) NSArray *entries; #end and my RootViewController.m : #import "RootViewController.h" #import "AppRecord.h" #import "Cell.h" #define kCustomRowCount 7 #interface RootViewController () <UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout> // the set of IconDownloader objects for each app #property (nonatomic, strong) NSMutableDictionary *imageDownloadsInProgress; #end #implementation RootViewController #pragma mark // ------------------------------------------------------------------------------- // viewDidLoad // ------------------------------------------------------------------------------- - (void)viewDidLoad { NSLog(#"inside class"); [super viewDidLoad]; // self.title = #"My Title"; //self.collectionView.delegate = self; //self.collectionView.dataSource=self; } - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{ NSUInteger count = [self.entries count]; NSLog(#"count: %lu", (unsigned long)count); if (count == 0) { return kCustomRowCount; } return count; } - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{ NSLog(#"inside cell"); AppRecord *appRecord = [self.entries objectAtIndex:indexPath.row]; Cell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"MY_CELL" forIndexPath:indexPath]; UIImage *truckImage = [[UIImage alloc] init]; truckImage = [UIImage imageNamed:#"Default.png"]; cell.imageView.image = truckImage; return cell; } #end now problem is none of my "cellForItemAtIndexPath" or "numberOfItemsInSection" or even "viewDidLoad" getting called and the output on Simulator is black screen. This is my reload section of AppDelegate class : __block ParseOperation *weakParser = parser; parser.completionBlock = ^(void) { if (weakParser.appRecordList) { dispatch_async(dispatch_get_main_queue(), ^{ RootViewController *rootViewController = (RootViewController*)[(UINavigationController*)self.window.rootViewController topViewController]; rootViewController.entries = weakParser.appRecordList; if(weakParser.appRecordList != nil) NSLog(#"weakParser.appRecordList is Not nill"); [rootViewController.collectionView reloadItemsAtIndexPaths:[rootViewController.collectionView indexPathsForVisibleItems]]; [rootViewController.collectionView reloadData]; }); } self.queue = nil; }; [self.queue addOperation:parser]; self.appListData = nil; }
Did you add the delegate and datasource connection for the collection view? It's the most common mistake, usually. They're commented out in code, I assume you did that via Storyboard
Do you have identifier on the storyboard cell?You can try to add [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:#"MY_CELL"]; in viewDidLoad Since your RootViewController inherit UICollectionViewController you don't need to add UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout at all
Instances of UICollectionViewCell showing behind other instances of UICollectionView
To preface, I have some experience with iOS development, but this is my first app that I am going to publish to the AppStore, and also my first experience utilizing UICollectionView. Here is a summary of what I am doing in my app: I have a paged UICollectionView which contains several fullscreen UICollectionViewCells. Within each of these cells is another UICollectionView ('subCollectionView'), containing UICollectionViewCells ('subCollectionViewCells') which currently have their label set to "Test Text". When scrolling to the second collectionViewCell, the subCollectionViewCells of the first collectionViewCell are visible behind the second collectionViewCell's subCollectionView. I have linked the images to what is happening here. The CollectionViewCell that includes the subCollectionView: [VLCategoryCollectionViewCell.h] #import <UIKit/UIKit.h> #interface VLCategoryCollectionViewCell : UICollectionViewCell #property (nonatomic, strong) NSString *imageName; #property (strong, nonatomic) IBOutlet UIView *moduleSubview; - (void)updateCell; #end [VLCategoryCollectionViewCell.m] #import "VLCategoryCollectionViewCell.h" #import "VLModuleViewController.h" #interface VLCategoryCollectionViewCell() #property (strong, nonatomic) IBOutlet UIImageView *imageView; #property (strong, nonatomic) IBOutlet UILabel *categoryName; #property (strong, nonatomic) VLModuleViewController *moduleViewController; #end #implementation VLCategoryCollectionViewCell - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { // Initialization code NSArray *arrayOfViews = [[NSBundle mainBundle] loadNibNamed:#"VLCategoryCollectionViewCell" owner:self options:nil]; if ([arrayOfViews count] < 1) { return nil; } if (![[arrayOfViews objectAtIndex:0] isKindOfClass:[UICollectionViewCell class]]) { return nil; } self = [arrayOfViews objectAtIndex:0]; } return self; } -(void)updateCell { _moduleViewController = [[VLModuleViewController alloc] initWithNibName:#"VLModuleViewController" bundle:nil]; _moduleViewController.view.frame = self.moduleSubview.bounds; [self.moduleSubview addSubview:_moduleViewController.view]; NSString *sourcePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"Assets"]; NSString *filename = [NSString stringWithFormat:#"%#/%#", sourcePath, self.imageName]; UIImage *image = [UIImage imageWithContentsOfFile:filename]; NSString *fileTitle = [[filename lastPathComponent] stringByDeletingPathExtension]; [self.categoryName setFont:[UIFont fontWithName:#"OpenSans-ExtraBold" size:72.0]]; [self.categoryName setText:fileTitle]; [self.imageView setImage:image]; [self.imageView setContentMode:UIViewContentModeScaleAspectFit]; } #end [VLModuleViewController.h] #import <UIKit/UIKit.h> #interface VLModuleViewController : UIViewController <UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout> #end [VLModuleViewController.m] #import "VLModuleViewController.h" #import "VLModuleCollectionViewCell.h" #interface VLModuleViewController () #property (nonatomic, strong) IBOutlet UICollectionView *moduleView; #property (nonatomic, strong) NSArray *moduleArray; #property (nonatomic) int currentIndex; #property (nonatomic) NSString *cellIdentifier; #end #implementation VLModuleViewController - (void)loadView { [super loadView]; NSArray *moduleArray = [[NSBundle mainBundle] pathsForResourcesOfType:#"png" inDirectory:#"Module_Tests"]; NSMutableArray *moduleImageArray = [[NSMutableArray alloc] initWithCapacity:[moduleArray count]]; for (NSString *path in moduleArray) { [moduleImageArray addObject:[UIImage imageWithContentsOfFile:path]]; } self.moduleArray = [NSArray arrayWithArray:moduleArray]; } - (void)viewDidLoad { [super viewDidLoad]; [self setupCollectionView]; UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init]; [flowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal]; [self.moduleView setPagingEnabled:YES]; [self.moduleView setCollectionViewLayout:flowLayout]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; } #pragma mark - #pragma mark UICollectionView methods - (void)setupCollectionView { [self.moduleView registerClass:[VLModuleCollectionViewCell class] forCellWithReuseIdentifier:#"VLModuleCollectionViewCell"]; UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init]; [flowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal]; [self.moduleView setCollectionViewLayout:flowLayout]; } - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView { return 1; } - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return [self.moduleArray count]; } - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { VLModuleCollectionViewCell *cell = (VLModuleCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:#"VLModuleCollectionViewCell" forIndexPath:indexPath]; cell.titleLabel.text = #"Module Title Goes Here"; return cell; } - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { return CGSizeMake(235, 147); } #end
I had a similar issue with labels overwriting themselves in collectionView, try calling your updateCell method in awakeFromNib, this did the trick for me.
cellForRowAtIndexPath doesn't work
On my xcode project I have a view with a tableview. I also have a "special" string that contains the objects to populate the array that populates the tableview. Example: NSString *myValues=[[NSString alloc]init]; myValues = #"aaa$bbb$ccc?111$222$333?"; as you can see the characters $ and ? separate the objects. When I call the view I populate the array by the string but the tableview doesn't work. Here some code: File.H: #import <UIKit/UIKit.h> #interface EquipaggioVC : UIViewController <UITableViewDelegate, UITableViewDataSource>{ UITableView *tableViewEquipaggio; UITableViewCell *nibLoadedCell; NSMutableArray *arrayEquipaggio; NSString *stringaDati; } #property (nonatomic, retain) IBOutlet UITableView *tableViewEquipaggio; #property (nonatomic, retain) IBOutlet UITableViewCell *nibLoadedCell; #property (nonatomic, retain) NSMutableArray *arrayEquipaggio; #property (nonatomic, retain) NSString *stringaDati; #end File.M: #import "EquipaggioVC.h" #import "AppDelegate.h" #import "ClassEquipaggio.h" #interface EquipaggioVC () #end #implementation EquipaggioVC #synthesize tableViewEquipaggio; #synthesize nibLoadedCell; #synthesize arrayEquipaggio; #synthesize stringaDati; - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; } -(NSString *)uuid { //to create an ID CFUUIDRef uuidRef = CFUUIDCreate(NULL); CFStringRef uuidStringRef = CFUUIDCreateString(NULL, uuidRef); CFRelease(uuidRef); return [(NSString *)uuidStringRef autorelease]; } -(void)loadTestData:(NSString*)stringaDatiArray{ //populate the array by the string. if(stringaDatiArray!=nil){ NSArray *elenco=[stringaDatiArray componentsSeparatedByString:#"?"]; for (int i=0; i<([elenco count]-1) ; i++) { NSString *dettagliEquipaggio=[[NSString alloc]init]; dettagliEquipaggio=[elenco objectAtIndex:i]; NSArray *dettagli=[dettagliEquipaggio componentsSeparatedByString:#"$"]; ClassEquipaggio *aEquipaggio=[[ClassEquipaggio alloc]init]; aEquipaggio.idMembro=[dettagli objectAtIndex:0]; aEquipaggio.sessoMembro=[dettagli objectAtIndex:1]; aEquipaggio.dataNascitaMembro=[dettagli objectAtIndex:2]; [arrayEquipaggio addObject:aEquipaggio]; [aEquipaggio release]; } } } - (UITableViewCell *)tableView:(UITableView *)tableEquipaggioView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = #"Cell"; static NSString *sLoadNibNamed; UITableViewCell *cell; ClassEquipaggio *aEquipaggio=[arrayEquipaggio objectAtIndex:indexPath.row]; cell = [tableEquipaggioView dequeueReusableCellWithIdentifier:CellIdentifier]; sLoadNibNamed=#"EquipaggioCell"; if (cell == nil) { [[NSBundle mainBundle] loadNibNamed:sLoadNibNamed owner:self options:NULL]; cell = [self typeNibLoadCell:aEquipaggio.idMembro]; } // Configure the cell UILabel *tipoSessoLabel = (UILabel*) [cell viewWithTag:1]; tipoSessoLabel.text = aEquipaggio.sessoMembro; UILabel *dataNascitaLabel=(UILabel*)[cell viewWithTag:2]; dataNascitaLabel.text=aEquipaggio.dataNascitaMembro; return cell; } -(NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section{ return [arrayEquipaggio count]; } -(UITableViewCell *)typeNibLoadCell:(NSString *)named{ return nibLoadedCell; } - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { // Return NO if you do not want the specified item to be editable. return NO; } - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } - (void)viewDidLoad { [super viewDidLoad]; arrayEquipaggio=[[NSMutableArray alloc]init]; stringaDati=[[NSString alloc]init]; tableViewEquipaggio=[[UITableView alloc]init]; } -(void)viewDidAppear:(BOOL)animated{ AppDelegate *appDelegate=(AppDelegate*)[[UIApplication sharedApplication]delegate]; UIBarButtonItem * btnIndietroa = [[[UIBarButtonItem alloc] initWithTitle:#"Indietro" style:UIBarButtonItemStyleBordered target:self action:#selector(editNavButtonPressed)]autorelease]; [self.navigationItem setLeftBarButtonItem:btnIndietroa]; stringaDati = [[NSUserDefaults standardUserDefaults] objectForKey:#"stringaDati"]; if(stringaDati!=nil){ [arrayEquipaggio removeAllObjects]; [self loadTestData:stringaDati]; } } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } -(void)dealloc{ [super dealloc]; } #end On nib file the tableview has the connection with the delegate and datasource, as the IBoutlet is connected with it. Thanks for your support. EDIT: [arrayEquipaggio count] return 0.
By the looks of your question your problem is that the method cellForRowAtIndexPath is not being called and as you said the [arrayEquipaggio count] return 0. Since you are using that array at the method numberOfRowsInSection with a return value of 0 is normal that your table is not calling the method cellForRowAtIndexPath. Since you are telling to your table that there are no rows. If you want your table to be populated then check that your array return a value greater than 0. In a nutshell It has no data to populate your table, that why it doesnt work. Check that in your viewDidAppear:(BOOL)animated that your are pulling anything to stringaDati. NSLog(#"info: %#",stringaDati);