gpt4 book ai didi

ios - 正在调用委托(delegate)方法但未正确执行

转载 作者:行者123 更新时间:2023-11-29 03:04:31 24 4
gpt4 key购买 nike

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

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;
}

如果有人可以帮助我解决这个问题,我将非常感激。

提前致谢克里斯

最佳答案

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

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

您可以在您的自定义单元格中尝试这样做:

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

当然,在 View Controller 中删除这些行。

关于ios - 正在调用委托(delegate)方法但未正确执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22937623/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com