Question

I have a collectionView of Photos, when the photo is tapped it should show a detail view of that photo.

The problem is, the first photo I tap doesn't do anything, and then every tap after works except it shows the photo tapped two times ago.

For example:

Tap Photo 1... does nothing.

Tap Photo 2... shows Photo 1.

Tap Photo 6... shows Photo 2.

Thanks for the help!

ViewController.m

@property (nonatomic) NSArray *photos;

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.collectionView registerClass:[BlissPhotoCell class] 
    forCellWithReuseIdentifier:@"photo"];

    NSURLSession *session = [NSURLSession sharedSession];
    NSString *urlString = [[NSString alloc] initWithFormat:@"https://api.instagram.com/v1/;
    NSURL *url = [[NSURL alloc]initWithString:urlString];
    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
    NSURLSessionDownloadTask *task = [session downloadTaskWithRequest:request completionHandler:^(NSURL *location, NSURLResponse *response, NSError *error) {
        NSString *text = [[NSString alloc] initWithContentsOfURL:location encoding:NSUTF8StringEncoding error:nil];

        NSData *data = [[NSData alloc] initWithContentsOfURL:location];
        NSDictionary *responseDictionary = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];

        self.photos = [responseDictionary valueForKeyPath:@"data"];
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.collectionView reloadData];
        });
    }];
    [task resume];

- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath {
    NSDictionary *photo = self.photos[indexPath.row];
    BlissDetailViewController *viewController = [[BlissDetailViewController alloc] init];

    viewController.modalPresentationStyle = UIModalPresentationCustom;
    viewController.transitioningDelegate = self;

    viewController.photo = photo;
    [self presentViewController:viewController animated:YES completion:nil];
}

BlissPresentDetailTransition.h

- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext {
    UIViewController *detail = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    UIView *containerView = [transitionContext containerView];

    detail.view.alpha = 0.0;
    CGRect frame = containerView.bounds;
    frame.origin.y += 20.0;
    frame.size.height -= 20.0;
    detail.view.frame = frame;
    [containerView addSubview:detail.view];

    [UIView animateWithDuration:0.3 animations:^{
        detail.view.alpha = 1.0;
    } completion:^(BOOL finished) {
        [transitionContext completeTransition:YES];
    }];
}

- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext {
    return 0.3;
}
Was it helpful?

Solution

You inadvertently implemented didDeselect rather than didSelect. It's an easy mistake to make if you use auto completion when you type since didDeselect comes up first alphabetically.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top