3

somehow my collectionView does not load my data. I fetch data from facebook and call reloadData after the array is populated, but nothing happens...

No Cell's are displayed, as the array is 0 when ViewDidLoad is called.

How do I reload that it take effect on the collectionView? What's wrong in my code?

CollectionViewController.h

#import <UIKit/UIKit.h>
#import <FacebookSDK/FacebookSDK.h>

@interface CollectionViewController : UICollectionViewController <UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>

@property (nonatomic, weak) IBOutlet UICollectionView *collectionView;
@property (nonatomic, strong) NSMutableArray *searches;
- (IBAction)getFotos:(id)sender; 
@end

CollectionViewController.m

#import "CollectionViewController.h"
#import "CollectionViewCell.h"
#import "BHPhoto.h"

@interface CollectionViewController () 

@end

@implementation CollectionViewController

@synthesize collectionView;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSMutableArray *searches = [[NSMutableArray alloc] init];
    self.searches = searches;

    [self getFotos:nil];
    NSLog(@"searches amount: %u", [self.searches count]);

    [self.collectionView registerClass:[CollectionViewCell class] forCellWithReuseIdentifier:@"collcell"];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - UICollectionView Datasource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    int ret = [self.searches count];   // <-- here the searches return 0 instead of the filled searches size, what I though will happen if reloadData is called
    NSLog(@"numberOfItemsinSection - I: %d", ret); 
    return ret;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    int ret = 1;
    NSLog(@"numberOfSection - S: %d", ret);
    return ret;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *cell = [cv dequeueReusableCellWithReuseIdentifier:@"collcell" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor whiteColor];
    return cell; 
}

#pragma mark - UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    // @todo select
}

- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath {
    // @todo de-select
}

#pragma mark - UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"SETTING SIZE FOR ITEM AT INDEX %d", indexPath.row);
    return CGSizeMake(80, 80);
}

#pragma mark -
#pragma mark get Facebook fotos around me
- (IBAction)getFotos:(id)sender {
    NSLog(@"getFotos in collview");
    [FBRequestConnection
     startWithGraphPath:@"search?type=place&center=43.119340,22.694992&distance=1000&fields=picture"
     completionHandler:^(FBRequestConnection *connection,
                         id result,
                         NSError *error) {
         if (!error) {
              [self readjson:result];
         }
     }];
}

#pragma mark read json for search location
- (IBAction)readjson:(NSDictionary *)jsondata {

    NSLog(@"keys: %@", [jsondata allKeys]);
    for (id item in [jsondata objectForKey:@"data"]) {        

        // add Photos to array
        NSURL *photoURL = item[@"picture"][@"data"][@"url"]; 
        BHPhoto *photo = [BHPhoto photoWithImageURL:photoURL];
        [self.searches addObject:photo];

    }
    NSLog(@"searches content: %@", self.searches);
    [self.collectionView reloadData]; // <-- reload data is called but does not have an effect...

}


@end

Debug output:

2013-01-11 00:23:46.308 foobar[3099:19a03] getFotos in collview
2013-01-11 00:23:46.309 foobar[3099:19a03] searches amount: 0
2013-01-11 00:23:46.312 foobar[3099:19a03] numberOfSection - S: 1
2013-01-11 00:23:46.312 foobar[3099:19a03] numberOfItemsinSection - I: 0
2013-01-11 00:23:46.591 foobar[3099:19a03] keys: (
    data,
    paging
)
2013-01-11 00:23:46.592 foobar[3099:19a03] searches content: (
    "<BHPhoto: 0x9351460>",
    "<BHPhoto: 0x938ad20>",
    "<BHPhoto: 0x938ade0>",
    "<BHPhoto: 0x938ae90>",
    "<BHPhoto: 0x938af40>",
    "<BHPhoto: 0x938b000>"
)

cheers

jerik
  • 5,714
  • 8
  • 41
  • 80
  • I think what is happening is that you are calling [self getFotos:nil] in viewDidLoad. There may be a timing issue when it comes to the collection view rendering and when the table view is rendered etc. Try forcing a call to [self getFotos:nil] through another button or do it in viewDidAppear. Based on the NSLog statements, you have shown, the tableview methods are not even called after getFotos is called. – Srikanth Jan 11 '13 at 01:30
  • I will try, but through reloaddata the data should be reloaded, when this is called. Also if i hardcode the items, so that items are displayed, at least the placeholder, the reloaddata has no effect. Btw. Its a collectionview, not tableview. Cheers – jerik Jan 11 '13 at 07:19
  • Your CollectionViewController, is it a subclass of UICollectionViewController. If yes, then did you add a Collectionview explicitly. You do not need to add a collection view explicitly in that case. Can you post the .h file of this class. – Srikanth Jan 11 '13 at 19:06
  • Yes is a subclass of UICollectionViewController. .h-file will follow as soon as my macbook is recovered. – jerik Jan 11 '13 at 22:48
  • Added the .h-file in the initial post. – jerik Jan 11 '13 at 23:00
  • You do not need to have another CollectionView and an outlet. That may be the problem. Remove the outlet and the entry in the xib file and check. – Srikanth Jan 11 '13 at 23:29
  • In your `cellForItemAtIndexPath` you'll have to define the cell contents. E.g. the image of the cell `[cell.imageViewProperty setImage:[UIImage ...]]` As value you need a UIImage representation of the photo stored in BHPhoto. – SAE Jan 12 '13 at 00:09
  • I defined the cell contents. Still run in the same issue, that the array is at the beginning empty and e.g. `collectionView numberOfItemsInSection:` return 0, so no placeholders are drawn. How can I ensure that the array is first read when it is populated with items? are `blocks`the way to go? cheers – jerik Jan 12 '13 at 21:22

1 Answers1

3

You forgot to set your class as its own delegate/datasource. In your controller class init, add those lines like this so it knows your protocol implementations are for itself:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.collectionview.delegate = self;
        self.collectionview.datasource = self;
    }
    return self;
}

This took me a while too but I noticed your debug output was only calling numberOfItemsInSection: once (on viewDidLoad). When you call [self.collectionview reloadData], it will call the method again so you should see another debugging message with the correct count.

myclues
  • 123
  • 5