我可以使用 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 {
}