我的集合视图单元格没有被调用?

时间:2014-04-26 15:54:03

标签: ios uicollectionview

我不确定为什么我的收藏视图不会调用我的单元格,不知道为什么?此外,它从来没有自动实现方法,我不得不添加他自己,部分的数量等。问题它显示一个没有单元格的空白控制器视图,或至少我的CollectionController.m文件中的方法做因为我有一个NSLog而没有被调用。这是我的代码。

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

我的故事板看起来像这样:

enter image description here

enter image description here

enter image description here

1 个答案:

答案 0 :(得分:1)

在您收到数据后,您需要在收集视图上调用reloadData,因为您正在进行异步下载。

[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);

}];