سؤال

This is the flow of my app: I have UICollectionview with thumbnails of images. User selects images and taps on Save button. Images get saved to camera roll.

This is what I want to do: With a subview I want to mark those cells that user has already tapped on and saved. How do I achieve that?

This is what I have already done....my approach: Obviously I'm missing something and I can't get it resolved.

When user taps on save button I successfully save the IDs of those images to local store using core data. Then, I fetch objects from that entity and put those objects in Array. Now, I'm trying to compare image IDs from fetched array with image IDs in collection view so I can only add subview to those cells where ID's match and that's where i get stuck. I don't know if that's the right approach.

My foolish attempt/s:

This method gets called when user taps on Save Button but it doesn't work properly. I feel as it's randomly adding subviews.

-(void)markSavedImages
{
    BOOL isMarked;

    NSArray *savedImages = [self fetchSavedPhotos];

    NSEnumerator *enumerator = [savedImages objectEnumerator];
    NSString *savedImage;
    while (savedImage = [enumerator nextObject]) {
        if ([[self.eventPhotos valueForKey:@"id"] containsObject:savedImage]) {
            isMarked = YES;
        }
        else
        {
            isMarked = NO;
        }
    }

    CVCell *cell = [[CVCell alloc]init];
    for (NSIndexPath *indexPath in self.livePhotosCollectionView.indexPathsForVisibleItems) {
        if (isMarked) {
            cell = (CVCell*)[self.livePhotosCollectionView cellForItemAtIndexPath:indexPath];

            UIImageView *markedImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"markedCVCell.png"]];
            [cell addSubview:markedImage];
            [cell bringSubviewToFront:markedImage];
            cell.userInteractionEnabled = NO;
        }
    }
}

The self.eventPhotos is an array that contains image's IDs and paths to server.

Please help me guys, I've been killing my self with this and couldn't find anything on google.

EDIT - as per request

-(NSArray*)fetchSavedPhotos
{
    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication]delegate];
    NSManagedObjectContext *context = [appDelegate managedObjectContext];
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Photos" inManagedObjectContext:context];
    [fetchRequest setEntity:entity];

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"eventID ==[c] %@", _eventID];
    [fetchRequest setPredicate:predicate];

    NSError *error = nil;
    NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];
    if (fetchedObjects == nil) {
        NSLog(@"No rows returned");
    }

    NSArray *savedImages = [fetchedObjects valueForKey:@"imageID"];
    return savedImages;

}

Some more information....if I output savedImages array in markSavedImages method i do get the array I want to get. Something like this: (1324,2353,2324). Basically array of image IDs.

هل كانت مفيدة؟

المحلول

To begin with, don't add views just to some cells. This usually results in the view being shown on reused cells and multiple copies of the view being added to the cells which looks confusing and is inefficient.

Instead, add the view to every cell. Do it in your cell subclass and make the view publicly visible or add a method to show and hide it. Then do just that. Every time you return a cell, set the visibility of your subview. This ensures that the cell is always configured correctly and that your configuration code is simple.

For disabling the cells use a similar approach, always choose of the cell is enabled or not and set the user interaction enabled on the cell as a whole.

For your data you have a list of the source image ids and a list of the saved image ids. When you configure the cell you know the associated image id. To check if the cell should be tagged and disabled you can do:

BOOL saved = [self.savedImages containsObject:imageId];

(Compare the current id to the list of selected ids, don't do any looping).

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top