Question

I'm having trouble calling a delegate method that resides in my UICollectionViewCell...

In my FOFPhotoCell.m i have the following code:

    -(void)fadeOutLabels
{
    NSLog(@"fadeOutLabels was called");
    [UIView animateWithDuration:1.0
                          delay:0.0  /* do not add a delay because we will use performSelector. */
                        options:UIViewAnimationOptionCurveEaseIn
                     animations:^ {
                         self.titleLabel.alpha = 0.0;
                     }
                     completion:^(BOOL finished) {
                         [self.titleLabel removeFromSuperview];

                     }];

}

in the FOFPhotoCell.h there is the following defined:

@protocol FOFPhotoCellDelegate <NSObject>

@required
-(void)fadeOutLabels;

@end


@interface FOFPhotoCell : UICollectionViewCell {
    id delegate;
}


@property (nonatomic, weak) id<FOFPhotoCellDelegate> delegate;

in my FOFPhotosViewController.h:

@interface FOFPhotosViewController : UICollectionViewController <FOFPhotoCellDelegate>


@end

and lastly, my FOFPhotosViewController.m:

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    FOFPhotoCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"photo" forIndexPath:indexPath];

    NSArray *photosArray = [self.dishes valueForKeyPath:@"avatar_url"];
    NSArray *nameArray = [self.dishes valueForKeyPath:@"name"];


//    NSLog(@"photoURL %@", _responseDictionary);
    cell.backgroundColor = [UIColor lightGrayColor];
    [cell.imageView setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"****",[photosArray objectAtIndex: indexPath.row]]]];
    cell.titleLabel.text = [NSString stringWithFormat:@"%@", [nameArray objectAtIndex:indexPath.row]];




    UILongPressGestureRecognizer *tapAndHold = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(fadeOutLabels)];

    tapAndHold.minimumPressDuration = 0.5;
    [self.collectionView addGestureRecognizer:tapAndHold];



    [self.collectionView reloadItemsAtIndexPaths:[NSArray arrayWithObject:indexPath]];

    return cell;
}

But this makes the app crash: -[FOFPhotosViewController fadeOutLabels]: unrecognized selector sent to instance

I can't seem to figure this one out, so I would really appreciate some help! Let me know if any more code is needed.

Thanks in advance Chris

Was it helpful?

Solution

fadeOutLables is a method of your cell, not of your view controller.

The error message is just right.

Assuming that everything else is right, change this line as follows:

UILongPressGestureRecognizer *tapAndHold = [[UILongPressGestureRecognizer alloc] initWithTarget:cell action:@selector(fadeOutLabels)];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top