【发布时间】: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] <FOFPhotoCell: 0x10977d790; baseClass = UICollectionViewCell; frame = (683.5 683; 340.5 340.5); hidden = YES; layer = <CALayer: 0x10977d8d0>> -
当然,不知道为什么我没想到...非常感谢@Justafinger。
标签: ios objective-c methods delegates