【问题标题】:Delegate method is being called but doesn't execute properly正在调用委托方法,但未正确执行
【发布时间】:2014-05-21 04:24:45
【问题描述】:

我的一个委托方法有问题。我有一个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;
}

如果有人能帮助我解决这个问题,我将不胜感激。

提前致谢 克里斯

【问题讨论】:

  • 你能显示你初始化长按识别器的部分吗?您是否尝试在self.titleLabel.alpha = 0.0; 行设置断点?
  • 长按 g.recognizer 在我的 FOFPhotosViewController.m 中初始化(应该是从底部算起的第 5 行)。通过在该行设置断点,我会寻找什么?
  • 你能在NSLog(@"fadeOutLabels was called");之后添加NSLog(@"%@",self);并打印结果吗?
  • 我得到以下输出:2014-04-08 14:40:22.917 DSK[83147:60b] &lt;FOFPhotoCell: 0x10977d790; baseClass = UICollectionViewCell; frame = (683.5 683; 340.5 340.5); hidden = YES; layer = &lt;CALayer: 0x10977d8d0&gt;&gt;
  • 当然,不知道为什么我没想到...非常感谢@Justafinger。

标签: ios objective-c methods delegates


【解决方案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];
        ....
    }
}

当然还要删除视图控制器中的那些行。

【讨论】:

    猜你喜欢
    • 2018-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多