【发布时间】:2013-07-04 16:55:51
【问题描述】:
我正在尝试调整 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 的 Photo 类中得到的图片替换本地创建的数组。
我正在尝试在 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 想要使用数组中的信息来填充单元格时,那里什么都没有。
我不知道我是否将代码放在了错误的位置,或者我是否使用了错误的查询。
你能在这里得到我的错误吗?
任何反馈都将不胜感激。
【问题讨论】:
-
在完成块中,在您的 collectionView 上调用 reloadData 方法将填充单元格。我不确定这是否是您正在寻找的答案,但它会起作用
-
嗨@gg13,谢谢,但这似乎有点奇怪。我正在打印检索到的对象的数量(使用 NSLog),我首先得到一个 0,然后在 reloadData 之后得到适当的数量。这是使用 UICollectionViewController 的常规方式吗?虽然我必须承认我还不是专家,但这似乎很奇怪。
-
发生的情况是,当您第一次打印出检索到的对象的数量时,Parse 尚未完成对对象的查询,因此它返回 0。同理,集合视图试图用一个空数组设置自己,这就是为什么你什么也得不到。然后,因为您在完成块中调用了 reloadData,所以查询已经完成,因此您有了对象,并且集合视图可以自行填充。它实际上与 UITableView 没有什么不同,因为它们也无法在没有任何对象的情况下自行填充。
标签: ios uicollectionview parse-platform pfquery