我正在尝试调整 UICollectionViewController 的使用,我正在填充一个本地图像数组以从 Parse 获取图像。
到目前为止,它非常简单。我的 NSArray 多次填充相同的本地图像:
testImages = [NSArray arrayWithObjects: @"thumbnail.jpg", @"thumbnail.jpg", @"thumbnail.jpg", @"thumbnail.jpg", @"thumbnail.jpg", @"thumbnail.jpg", @" thumbnail.jpg", @"thumbnail.jpg", @"thumbnail.jpg", @"thumbnail.jpg", @"thumbnail.jpg", @"thumbnail.jpg", @"thumbnail.jpg", @"thumbnail .jpg", @"thumbnail.jpg", @"thumbnail.jpg", nil];
在 collectionView:cellForItemAtIndexPath: 我确实设置了我的单元格(来自 Storyboard):
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
// Set up cell identifier that matches the Storyboard cell name
static NSString *identifier = @"Cell";
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
// Configure the cell to show photo thumbnail
UIImageView *testImageView = (UIImageView *)[cell viewWithTag:100];
testImageView.image = [UIImage imageNamed:[testImages objectAtIndex:indexPath.row]];
return cell;
}
这是有效的,看起来像这样:
我想要做的是用我从 Parse 的照片类中得到的图片替换本地创建的数组。
我正在尝试在 viewDidLoad 方法上执行此操作:
PFQuery *query = [PFQuery queryWithClassName:@"Photo"];
PFUser *user = [PFUser currentUser];
[query whereKey:@"user" equalTo:user];
[query orderByAscending:@"createdAt"];
[query setCachePolicy:kPFCachePolicyNetworkOnly];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
// The find succeeded.
NSLog(@"Successfully retrieved %d photos.", objects.count);
testImages = [NSMutableArray arrayWithArray:objects];
NSLog(@"# Images: %d", [testImages count]);
// Do something with the found objects
for (PFObject *object in objects) {
NSLog(@"Object Name: %@", object.objectId);
}
} else {
// Log details of the failure
NSLog(@"Error: %@ %@", error, [error userInfo]);
}
}];
问题是我每次都得到一个空数组。我猜想,因为一旦 collectionView:numberOfItemsInSection: 询问“testImages”数组上的元素计数,我总是得到 0 个元素,因为这是在后台的块上执行的。
当 UICollectionViewController 想要使用数组中的信息来填充单元格时,那里什么都没有。
我不知道我是否将代码放在错误的位置,或者我是否使用了错误的查询。
你能在这里得到我的错误吗?
任何反馈将不胜感激。