どういうわけか私のcollectionViewは私のデータをロードしません。Facebook からデータを取得し、配列にデータが入力された後に reloadData を呼び出しましたが、何も起こりません...
ViewDidLoad が呼び出されたときに配列が 0 であるため、セルは表示されません。
collectionView で有効になるようにリロードするにはどうすればよいですか? 私のコードで何が問題になっていますか?
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¢er=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
デバッグ出力:
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>"
)
乾杯