Question

I'm unsure why my collection view does not call my cell, not sure why? Also, it never auto-implemented the methods, I had to add them my self, the numbers of section etc. The issue it shows a blank controller view with no cells, or at least the methods in my CollectionController.m file do not get called as I have an NSLog in it. Here is my code.

CollectionViewController.h:

#import <UIKit/UIKit.h>
#import "CollectionGridCell.h"

@interface HomeCollectionViewController : UICollectionViewController
<UICollectionViewDataSource, UICollectionViewDelegate>

@property (strong, nonatomic) NSMutableArray *homeImages;

@end

CollectionController.m:

#import "HomeCollectionViewController.h"
#import "SlidingMenuViewController.h"
#import "AFNetworking.h"
#import "CollectionGridCell.h"
#import <SDWebImage/UIImageView+WebCache.h>

@implementation HomeCollectionViewController

@synthesize homeImages;

- (void)viewDidLoad
{
    [super viewDidLoad];

    //add navigation top right bar items
    UIBarButtonItem *shareItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:
        UIBarButtonSystemItemAction target:self action:nil];
    UIBarButtonItem *cameraItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:
        UIBarButtonSystemItemCamera target:self action:nil];

    //add navigation top left bar items
    UIBarButtonItem *menuBtn = [[UIBarButtonItem alloc] initWithImage:
        [UIImage imageNamed:@"menuButton.png"] style:self target:self
        action:@selector(drawerAnimations)];

    //add items to array
    NSArray *actionButtonItems = @[shareItem, cameraItem];
    NSArray *leftActionButtonItems = @[menuBtn];

    //add array to navigation bar
    self.navigationItem.rightBarButtonItems = actionButtonItems;
    self.navigationItem.leftBarButtonItems = leftActionButtonItems;
    // Do any additional setup after loading the view.

    //grab content
    [self getHomeData];
}

- (void)drawerAnimations
{
    SlidingMenuViewController *slidingClass = [[SlidingMenuViewController alloc] init];
    [slidingClass createMenuButton];
}

- (void)getHomeData
{
    NSArray *categoriesSelected = [[NSUserDefaults standardUserDefaults]
        objectForKey:@"categories"];

    NSError* error;
    NSString *userId = [[NSUserDefaults standardUserDefaults] objectForKey:@"userId"];

    NSData *jsonData2 = [NSJSONSerialization dataWithJSONObject:categoriesSelected
        options:NSJSONWritingPrettyPrinted error:&error];
    NSString *jsonString = [[NSString alloc] initWithData:jsonData2
        encoding:NSUTF8StringEncoding];

    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    NSDictionary *parameters = @{@"userId": userId, @"categories": jsonString};
    manager.requestSerializer = [AFJSONRequestSerializer serializer];

    [manager GET:@"http://example.com/home" parameters:parameters
     success:^(AFHTTPRequestOperation *operation, id responseObject)
     {
         homeImages = [responseObject valueForKeyPath:@"pic"];
         NSLog(@"JSON: %@", homeImages);   
     }
     failure:^(AFHTTPRequestOperation *operation, NSError *error)
     {
        NSLog(@"Error: %@", error);
    }];
}

//sections to be displayed
#pragma mark -
#pragma mark UICollectionViewDataSource

-(NSInteger)numberOfSectionsInCollectionView:
(UICollectionView *)collectionView
{
    return 1;
}

//number of items in each section
-(NSInteger)collectionView:(UICollectionView *)collectionView
    numberOfItemsInSection:(NSInteger)section
{
    return homeImages.count;
}

//setting up each cell
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
                 cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CollectionGridCell *myCell = [collectionView dequeueReusableCellWithReuseIdentifier:
        @"GridCell" forIndexPath:indexPath];

    UIImage *image;
    long row = [indexPath row];

    image = [UIImage imageNamed:homeImages[row]];
    NSString *url = @"http://pics.example.com/";
    NSString *imageUrl = [NSString stringWithFormat:@"%@%@", url, image];

    // Here we use the new provided setImageWithURL: method to load the web image
    [myCell.homeImage setImageWithURL:[NSURL URLWithString:imageUrl]
                     placeholderImage:[UIImage imageNamed:@"menuButton.png"]];

   // myCell.homeImage.image = imageUrl;
    NSLog(@"the image is @%", imageUrl);

    return myCell;
}

@end

My storyboard looks like this:

enter image description here

enter image description here

enter image description here

Was it helpful?

Solution

You need to call reloadData on your collection view after you receive the data since you're doing an asynchronous download.

[manager GET:@"http://example.com/home" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {

    homeImages = [responseObject valueForKeyPath:@"pic"];
    [self.collectionView reloadData];
    NSLog(@"JSON: %@", homeImages);

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {

    NSLog(@"Error: %@", error);

}];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top