Instagram API を使用して必要なものを取得できますが、AFNetworking は非同期で動作するため、コードは CollectionView メソッドに直接ジャンプしますが、AFNetworking 呼び出しはまだ機能しており、CollectionView セルは読み込まれません。アレイの準備ができていません。[操作waitUntilFinished]を使用しました。しかし、何もしていないようです。
ここにコード、ありがとう!
-(void)viewDidLoad {
[super viewDidLoad];
NSString *path = @"https://api.instagram.com/v1/tags/snow/media/recent?access_token=836379.f59def8.fd07b9ba8ea440188dc56d2763bbf6c2";
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:path]];
AFJSONRequestOperation *operation =
[AFJSONRequestOperation JSONRequestOperationWithRequest:request
success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
imageArray = [[[[JSON valueForKey:@"data"]valueForKey:@"images"]valueForKey:@"low_resolution"]valueForKey:@"url"];
for (id item in imageArray) {
[images addObject:item];
}
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON){
// NSLog(@"There was a problem: %@", [error localizedDescription]);
}];
[operation start];
[operation waitUntilFinished]; // DOES NOT STOP AND WAIT
}
- (NSInteger)numberOfSectionsInCollectionView: (UICollectionView *)collectionView {
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection:(NSInteger)section {
/* RETURNS ARRAY COUNT OF ZER0 HERE, BUT I AM EXPECTING A VALID ARRAY OF URLS!
return [imageArray count];
}
- (CollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath {
CollectionViewCell *cell = [cv dequeueReusableCellWithReuseIdentifier:@"CellZero" forIndexPath:indexPath];
return cell;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
}