正在调用Delegate方法但不能正确执行

时间:2014-04-08 12:50:47

标签: ios objective-c methods delegates

我的某个委托方法有问题。我有一个collectionViewController,我添加了一个UILongPressGestureRecognizer,它在我的UICollectionViewCell中调用委托方法fadeOutLabels。我可以确认NSLog语句NSLog(@"fadeOutLabels was called");调用委托方法。但是该函数内的其他代码没有被执行。我很确定我错过了一些完全明显的东西,但我似乎无法自己解决这个问题。代码如下:

FOFPhotoCell.h

@protocol FOFPhotoCellDelegate <NSObject>

@required
-(void)fadeOutLabels;

@end


@interface FOFPhotoCell : UICollectionViewCell {
    id delegate;
}


@property (nonatomic, weak) id<FOFPhotoCellDelegate> delegate;
@property (nonatomic) UIImageView *imageView;
@property (nonatomic) NSDictionary *photo;
@property (nonatomic) NSArray *fetchPhotos;

@property (nonatomic) UILabel *titleLabel;





@end

FOFPhotoCell.m

@implementation FOFPhotoCell

@synthesize delegate = _delegate;


- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {

        CGFloat widthFromBounds = self.contentView.bounds.size.width;

        self.imageView = [[UIImageView alloc] init];
        [self.contentView insertSubview:self.imageView atIndex:0];


        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 30, widthFromBounds, 60)];
        self.titleLabel = titleLabel;
        self.titleLabel.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.3];
        self.titleLabel.font = [UIFont preferredFontForTextStyle:UIFontTextStyleHeadline];
        self.titleLabel.textColor = [UIColor whiteColor];
        self.titleLabel.textAlignment = NSTextAlignmentCenter;

        [self.contentView insertSubview:self.titleLabel atIndex:1];

    }
    return self;

}

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

                     }];

    }

FOFPhotosViewController.h

#import <UIKit/UIKit.h>
#import "FOFPhotoCell.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:@"http://xx.yy.zz.qq:4000%@",[photosArray objectAtIndex: indexPath.row]]]];
    cell.titleLabel.text = [NSString stringWithFormat:@"%@", [nameArray objectAtIndex:indexPath.row]];




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

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



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

    return cell;
}

如果有人帮我解决这个问题,我真的很感激。

提前致谢 克里斯

1 个答案:

答案 0 :(得分:1)

我相信您的问题来自LongPressGestureRecognizer,它应该在自定义单元格中实例化,而不是在视图控制器中。

基本上,如果你有一百个单元格,那么长按手势识别器应该知道你按下了哪个单元格以及哪个标签要淡出。

您可以在自定义单元格中尝试此操作:

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        UILongPressGestureRecognizer *tapAndHold = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(fadeOutLabels)];

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

当然,在视图控制器中删除这些行。