我正在尝试调整 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”数组上的元素计数,就会在后台的 block 上执行此操作,因此我总是得到 0 个元素。
当 UICollectionViewController 想要使用数组中的信息来填充单元格时,那里什么也没有。
我不知道我是否将代码放置在错误的位置,或者是否使用了错误的查询。
你能在这里得到我的错误吗?
如有任何反馈,我们将不胜感激。
最佳答案
终于我有了一些工作。 @gg13 的反馈是解决空数组问题的关键。
我会将解决方案留在这里,以防其他时候对其他人有所帮助。
我将查询放在一个新方法上:
- (void)queryForTable
{
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);
[self.collectionView reloadData];
gridImages = [[NSMutableArray alloc] initWithCapacity:objects.count];
// Do something with the found objects
for (PFObject *object in objects) {
PFFile *thumbnail = [object objectForKey:@"thumbnail"];
[thumbnail getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
if (!error) {
// Now that the data is fetched, update the cell's image property with thumbnail
NSLog(@"Fetching image..");
[gridImages addObject:[UIImage imageWithData:data]];
NSLog(@"Size of the gridImages array: %d", [gridImages count]);
} else {
// Log details of the failure
NSLog(@"Error: %@ %@", error, [error userInfo]);
}
}];
}
} else {
// Log details of the failure
NSLog(@"Error: %@ %@", error, [error userInfo]);
}
}];
}
我正在调用 viewDidLoad:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
[self queryForTable];
}
然后我将 collectionView:cellForItemAtIndexPath: 方法更改为:
- (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 *imageView = (UIImageView *)[cell viewWithTag:100];
//NSLog(@"testImages: %@", [testImages objectAtIndex:indexPath.row]);
imageView.image = [UIImage imageNamed:@"placeholder.jpg"];
imageView.image = [gridImages objectAtIndex:indexPath.row];
return cell;
}
到目前为止,它正在发挥作用。该应用程序正在从 Parse 获取图像,如下所示:
我仍然需要测试一些东西,特别是具有大量图像的场景(包括进度指示器或其他东西),但至少得到这个已经是一件好事。
我确信 Parse 迟早会制作自己的 UICollectionViewController,就像他们对 UITableViewController 所做的那样,而且事情会得到更多改善。
如前所述,感谢您的反馈,我将其留在这里以防其他人遇到同样的问题,当然也欢迎反馈和建议。
干杯,
关于ios - 在 ViewDidLoad 中解析 UICollectionViewController 的查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17475224/