调用委托方法UICollectionViewCell

时间:2014-04-06 18:36:39

标签: ios objective-c delegates uicollectionview uicollectionviewcell

我无法调用驻留在我的UICollectionViewCell中的委托方法......

在我的FOFPhotoCell.m中,我有以下代码:

    -(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];

                     }];

}
在FOFPhotoCell.h中,

定义如下:

@protocol FOFPhotoCellDelegate <NSObject>

@required
-(void)fadeOutLabels;

@end


@interface FOFPhotoCell : UICollectionViewCell {
    id delegate;
}


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

在我的FOFPhotosViewController.h中:

@interface FOFPhotosViewController : UICollectionViewController <FOFPhotoCellDelegate>


@end

最后,我的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;
}

但这会导致应用崩溃:-[FOFPhotosViewController fadeOutLabels]: unrecognized selector sent to instance

我似乎无法想出这个,所以我真的很感激一些帮助!如果需要更多代码,请告诉我。

提前致谢 克里斯

1 个答案:

答案 0 :(得分:0)

fadeOutLables是您单元格的一种方法,而不是视图控制器的方法。

错误信息恰到好处。

假设其他一切都是正确的,请按以下步骤更改此行:

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