简体   繁体   中英

How to retrieve certain images from Parse.com

I load a "Rooms" UICollectionView with specific images that the logged in user has selected in a previous view controller, by populating the "imageFilesArray" and telling the UICollectionViewCells to use its data:

-(void) retrieveSelectedImagesForRooms
{
//parse query where we search the selectedImage array column and return any entry where the array contains the logged in user objectid

   PFQuery *getRooms = [PFQuery queryWithClassName:@"collectionViewData"];
   [getRooms whereKey:@"selectedImage" equalTo:[PFUser currentUser].objectId];

   [getRooms findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
    if (!error)
    {
        imageFilesArray = [[NSArray alloc] initWithArray:objects];
        [roomsCollection reloadData];
    }
}];
}

The next page has to show the specific lights that user has selected for that previously selected room image. So I add the row's objectid I've just selected to a new column on Parse, called "clickedRoom", when the room is selected:

-(void)selectedRoom:(PFObject*)object
{
    [object addUniqueObject:object.objectId forKey:@"clickedRoom"]; //put object id into clickedRoom column on Parse to save what room you specifically chose so that the light images correspond to only that room

    [object saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error)
    {
    if (!error){}
    else{}
}];
}


- (void)collectionView:(UICollectionView*)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {

   [self selectedRoom:[imageFilesArray objectAtIndex:indexPath.row]];
   [self performSegueWithIdentifier:@"myLights" sender:self];

}

Now, in the "Lights" page I need to show ONLY the light images that have the selected room's objectid in that "clickedRoom" column. I believe it's the same principle as how I retrieve the room images, but I can't figure out what I should be querying for, something like:

-(void) retrieveCorrespondingImagesForLights
{
   PFQuery *getLights = [PFQuery queryWithClassName:@"collectionViewData"];
   [getLights whereKey:@"clickedRoom" equalTo:**MY-PREVIOUSLY-SELECTED-ROW**.objectid];
   [getLights findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
    if (!error)
    {
        imageFilesArray = [[NSArray alloc] initWithArray:objects];
        [myLightsCollection reloadData];
    }
}];

}

Any suggestions please?!

The retrieveCorrespondingImagesForLights is in a different view controller than your roomsCollection, correct? If so, then you will need to pass the object id of the selected room to the new view controller that is segued to at [self performSegueWithIdentifier:@"myLights" sender:self];

Take a look here Pass Index Number between UITableView List segue

In your case, you should add a property to your destination view controller (I'll call it LightsViewController) to capture the object, or objectId if that's all you need for the query. I would suggest something like this:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{   
  if ([segue.identifier isEqualToString:@"myLights"]) {

    // note that "sender" will be the cell that was selected
    UICollectionViewCell *cell = (UICollectionViewCell*)sender;
    NSIndexPath *indexPath = [roomsCollection indexPathForCell:cell];

    LightsViewController *vc = (LightsViewController*)[segue destinationViewController];
    vc.selectedObject = indexPath.row;
  }
}

Then, in retrieveCorrespondingImagesForLights:

PFQuery *getLights = [PFQuery queryWithClassName:@"collectionViewData"];
[getLights whereKey:@"clickedRoom" equalTo:self.selectedObject.objectid];

EDIT*

Without understanding your exact implementation details, it seems like you are trying to use Parse to pass data between your view controllers when you'd be better suited to do it natively in your app. Maybe I'm misunderstanding your issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM