目前,我正在尝试从UICollectionView中的解析查询实现我的图像。最初我绑这样做:
这是我的viewDidLoad
:
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(@"View Did Load");
// Do any additional setup after loading the view, typically from a nib.
PFQuery *query = [PFQuery queryWithClassName:@"SwapPost"];
self.dataArray = [query findObjects];
[self setupCollectionView];
}
这是我的cellForItemAtIndexPath
:
-(CMFGalleryCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"Row: %ld",(long)indexPath.row);
CMFGalleryCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath];
NSString *imageName = @"Picture";
if(imageName == nil){
NSLog(@"imageName is nil");
}
[cell setImageName:imageName];
PFFile *file = [[self.dataArray objectAtIndex:indexPath.row] objectForKey:@"image"];
NSLog(@"filename = %@",[file name]);
cell.imageView.file = file;
[cell.imageView loadInBackground];
return cell;
}
但由于某种原因,即使我的一些调试代码输出检索到19个对象,但只有一个图像显示应该有19个(每个对象有一个PFFile图像)
然后,我将后一种方法改为:
-(CMFGalleryCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"Row: %ld",(long)indexPath.row);
CMFGalleryCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath];
PFQuery *newQuery =[PFQuery queryWithClassName:@"SwapPost"];
NSArray *newArray = [newQuery findObjects];
NSLog(@"%@",[[newArray objectAtIndex:indexPath.row] objectId]);
NSString *imageName = @"Picture";
if(imageName == nil){
NSLog(@"imageName is nil");
}
[cell setImageName:imageName];
PFFile *file = [[newArray objectAtIndex:indexPath.row] objectForKey:@"image"];
NSLog(@"filename = %@",[file name]);
cell.imageView.file = file;
[cell.imageView loadInBackground];
return cell;
}
这显示了我想要的所有19个图像..然而,我实现了一个水平图像滚动,一次一个图像(一次一个单元格),这意味着每次用户滚动查询时都会调用findObjects
一遍又一遍地在网络上变得非常繁琐并且使应用程序滞后。无论如何要修复它,以便我只需要拨打findObjects
一次,然后我就能顺利显示所有19张图像吗?
答案 0 :(得分:2)
我认为你对UICollectionView
的工作原理有误解。获得用于数据源(UICollectionViewDataSource
协议)的数据后,除非后备数据发生更改,否则不会重新加载。
根据您所说的内容,以下是我认为您的代码应该是什么样子的示例。请注意:我无法弄清楚imageName
会怎么做,因为您在创建时会立即分配它,然后针对nil
对其进行测试,根据您的代码,这将永远不会成立。因此,我删除了那一点。
- (void)viewDidLoad {
[super viewDidLoad];
[self runQuery];
}
- (void)runQuery {
PFQuery *query = [PFQuery queryWithClassName:@"SwapPost"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
// You obviously need better error handling in here...
self.dataArray = objects;
[self.collectionView reloadData];
}
];
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
// This is an assumption
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return self.dataArray.count;
}
- (CMFGalleryCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
CMFGalleryCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath];
PFObject *post = self.dataArray[indexPath.row];
PFFile *file = post[@"image"];
cell.imageView.file = file;
[cell.imageView loadInBackground];
return cell;
}
现在,这不是完整的代码。它可以工作,但它没有经过优化,如果你没有从Parse得到任何回报(或得到错误),它就没有错误处理。此外,根据查询所用的时间长短,您的UI将不会向用户显示任何内容,表明正在进行中。我通常使用MBProgressHUD
的实例来阻止UI并指示正在进行查询。抛开这些警告,这应该可以让你开始。