我对如何从选定的行获取特定列感到困惑。
我的第一个View是一个UICollectionView,它可以查找Class中的所有对象。选择一个单元格后,您将被一个包含另一个UICollectionView的DetailView拦截,该另一个UICollectionView将显示该选定行的文件。
如何设置查询以便第二个CollectionView显示特定行的特定列?
请看图片。我需要在第二个CollectionView中显示image_1,image_2,image_3 ......等等。
已编辑代码:
VestimentaViewController.m
#import "VestimentaViewController.h"
#import "VestimentaDetailViewController.h"
@interface VestimentaViewController ()
@end
@implementation VestimentaViewController
@synthesize imagesCollection, activityIndicator;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
[self queryParseMethod];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)queryParseMethod {
[activityIndicator startAnimating];
PFQuery *query = [PFQuery queryWithClassName:@"VestimentaView"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
vestimentaFilesArray = [[NSArray alloc] initWithArray:objects];
[imagesCollection reloadData];
}
}];
}
#pragma mark Collection data source
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection: (NSInteger)section {
return [vestimentaFilesArray count];
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = @"Cell";
VestimentaCell *cell = (VestimentaCell *) [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
PFObject *infoObject = [vestimentaFilesArray objectAtIndex:indexPath.row];
cell.storeName.text = [infoObject objectForKey:@"cellTitle"];
cell.storeDescription.text = [infoObject objectForKey:@"descriptionTitle"];
[activityIndicator stopAnimating];
}
return cell;
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"vestimentaDetail"]){
NSIndexPath *indexPath = [self.imagesCollection indexPathForCell:sender];
PFObject *object = [vestimentaFilesArray objectAtIndex:indexPath.item];
VestimentaDetailViewController *detailViewController = segue.destinationViewController;
detailViewController.vestimenta = object;
}
}
@end
VestimentaViewController.h
#import <UIKit/UIKit.h>
#import "VestimentaCell.h"
#import <Parse/Parse.h>
@interface VestimentaViewController : UIViewController {
NSArray *vestimentaFilesArray;
NSMutableArray *tiendasArray;
}
@property (weak, nonatomic) IBOutlet UICollectionView *imagesCollection;
@property (weak, nonatomic) IBOutlet UIActivityIndicatorView *activityIndicator;
@end
VestimentaDetailViewController.m
#import "VestimentaDetailViewController.h"
#import "VestimentaViewController.h"
@interface VestimentaDetailViewController ()
@end
@implementation VestimentaDetailViewController
@synthesize imagesCollection;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
[self queryParseMethod];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark Collection data source
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return [filesArray count];
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = @"Cell";
VestimentaDetailCell *cell = (VestimentaDetailCell *) [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
PFFile *imageFile = [self.vestimenta objectForKey:@"image_1"];
[imageFile getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
if (!error) {
cell.imageFile.image = [UIImage imageWithData:data];
}
}];
return cell;
}
@end
VestimentaDetailViewController.h
#import <UIKit/UIKit.h>
#import "VestimentaDetailCell.h"
#import <Parse/Parse.h>
@interface VestimentaDetailViewController : UIViewController {
NSArray *filesArray;
}
@property (weak, nonatomic) PFObject *vestimenta;
@property (weak, nonatomic) IBOutlet UICollectionView *imagesCollection;
@end
这将显示每个单元格的图像Clicked但仅显示image_1 ...如何构建将显示任何单击单元格的image_1,image_2,image_3的数组或字典(在第二个或详细视图控制器中)?
答案 0 :(得分:2)
您可以编写一个函数,该函数在选择行时调用,该函数将触发查询并在第二个ViewController中放置在imageview(PFImageView)上,如
-(void)settingAllImageView
{
PFQuery *query = [PFQuery queryWithClassName:@"ImageClassName"];
/*Could put up condition if require specific row*/
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error)
{
if (!error && objects.count != 0)
{
NSLog(@"Successfully retrieved: %@", objects);
for(int *i=0;i<=[objects count];i--)
{
NSDictionary *dict = [objects objectAtIndex:i];
PFFile *file = [object objectForKey:@"image"];
imageView.file = file;
/*imageView is PFImageView*/
/*For more than 1 images in a row you could store PFFiles in a array den use that array to load up imageView*/
}
// Now tell PFImageView to download the file asynchronously
[imageView loadInBackground];
}
}];
}
答案 1 :(得分:0)
您不从Parse获取特定列。您始终将整行作为PFObject获取,然后从该对象获取特定属性。
在您的集合视图中,您已经显示了cellTitle。这意味着您已经拥有包含图像的PFObject。您应该使用PFImageView来显示图像。这些文件尚未下载,因此您需要做的是将PFImageView文件设置为image_1文件等,如下所示:
PFImageView *firstImage = [[PFImageView alloc] init];
firstImage.file = theObjectFromPreviousCollectionView[@"image_1"];
[firstImage loadInBackground];
“theObjectFromPreviousCollectionView”是与您单击的单元格对应的对象。在prepareForSegue方法中,将此对象传递给详细信息视图。