[英]Calling delegate method UICollectionViewCell
我在調用駐留在我的UICollectionViewCell中的委托方法時遇到麻煩...
在我的FOFPhotoCell.mi中,具有以下代碼:
-(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
我似乎無法弄清楚這一點,因此,我將非常感謝您的幫助! 讓我知道是否需要更多代碼。
提前感謝克里斯
fadeOutLables
是單元格的一種方法,而不是視圖控制器的一種方法。
錯誤消息是正確的。
假設其他一切都正確,請按如下所示更改此行:
UILongPressGestureRecognizer *tapAndHold = [[UILongPressGestureRecognizer alloc] initWithTarget:cell action:@selector(fadeOutLabels)];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.