集合视图中的点击手势无法执行

Posted

技术标签:

【中文标题】集合视图中的点击手势无法执行【英文标题】:Tap gesture in collection view not able to perform 【发布时间】:2017-06-14 11:38:47 【问题描述】:

在我点击图片之前一切正常 这是我的日志--

2017-06-14 16:58:09.451 滑块菜单[3136:252131] -[DrinkViewController onButtonTapped:]:无法识别的选择器发送到实例 0x7ff025d50cd0 2017-06-14 16:58:09.460 滑块 Menu[3136:252131] *** 由于未捕获的异常而终止应用程序 'NSInvalidArgumentException',原因:'-[DrinkViewController onButtonTapped:]: 无法识别的选择器发送到实例 0x7ff025d50cd0

这是我的自定义 cell.m 文件(colcell.m)

导入“ColCell.h”

@implementation ColCell
-(void)awakeFromNib 
UITapGestureRecognizer * tap=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onButtonTapped:)];
[tap setNumberOfTapsRequired:1];
[self addGestureRecognizer:tap];

[super awakeFromNib];




-(void)onButtonTapped:(id)sender



//the response to the gesture.
//mind that this is done in the cell. If you don't want things to happen from this cell.
//then you can still activate this the way you did in your question.


这是我的收藏视图方法

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath




ColCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];


if(self.iname!=0)
    
    [cell setTag:indexPath.row];    // set tag to the indexPath.row so we can access it later
    
    // add interactivity
    UITapGestureRecognizer * tap=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onButtonTapped:)];
    
    [tap setNumberOfTapsRequired:1];
    
    [cell addGestureRecognizer:tap];
    
    NSString *fileName = [NSString stringWithFormat:@"%@",self.iname]; //objectAtIndex:indexPath];
    
    NSLog(@"%@",fileName);
    
    NSString *baseurl=[NSString stringWithFormat:@"http://test.kre8tives.com/barebon/upload/"];
    
    NSDictionary *dict = self.iname[indexPath.row];
    
    NSLog(@"%@", [self.iname objectAtIndex: indexPath.row]);
    
    NSString *paths = [NSString stringWithFormat:@"%@%@", baseurl, dict];
    
    NSLog(@"@@@@%@",paths);
    
    [NSURLConnection sendAsynchronousRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:paths]] queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) 
        
        NSLog(@"%@",response);
        
        
        UIImage *imgage = [[UIImage alloc] initWithData:data];
        UIImageView *dimg = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,100,100)];
        dimg.clipsToBounds = YES;
        [cell.dimg setImage:imgage];
        
        
        if(cell.dimg==!nil)
        
            NSLog(@"Not nil");
        
        else
            NSLog(@"nil");
        
        if(cell.lbl==!nil)
        
            NSLog(@"Not nil");
        
        else
            NSLog(@"nil");
        
        
        cell.dimg.image=imgage;
        cell.lbl.text=[self.tempz objectAtIndex:indexPath.row];
        self.catid=[self.categoryid objectAtIndex:indexPath.row];
        NSLog(@"%@",[self.iname objectAtIndex:indexPath.row]);
    ];

    //if (cell.selected) 
 //   cell.backgroundColor = [UIColor blueColor]; // highlight selection
  // 
 // else
  // 
    // cell.backgroundColor = [UIColor redColor]; // Default color
  // 
   return cell;
 

 -(void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath 
UICollectionViewCell *datasetCell =[collectionView cellForItemAtIndexPath:indexPath];
datasetCell.backgroundColor = [UIColor redColor]; // Default color
[self performSegueWithIdentifier:@"d2" sender:self];

我的目标是执行 segue 图像被点击并通过 segue 发送数据,但我的问题是,如果我点击图像我得到一个错误,我有所有的连接和一切工作 gr8!!

【问题讨论】:

改变你的手势目标 在哪里声明onButtonTapped: 不需要这个手势-(void)awakeFromNib UITapGestureRecognizer * tap=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onButtonTapped:)]; [tap setNumberOfTapsRequired:1]; [self addGestureRecognizer:tap]; [super awakeFromNib]; 我试图在 colcell 文件中执行 segue,但它不起作用!!!谢谢大家的帮助!!! @Akshay - 一旦调用了错误的方法,请查看 collectionview 委托 【参考方案1】:
   UIImageView *dimg = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,100,100)];
    dimg.clipsToBounds = YES;
    [cell.dimg setImage:imgage];
    [cell.dimg  setUserInteractionEnabled:YES];
 [cell.dimg setTag:indexPath.row];    // set tag to the indexPath.row so we can access it later

// add interactivity
UITapGestureRecognizer * tap=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onButtonTapped:)];

[tap setNumberOfTapsRequired:1];

[cell.dimg addGestureRecognizer:tap];

并处理类似的动作

- (void)onButtonTapped:(UITapGestureRecognizer *)gestureRecognizer 
 //UIImageView *myImage = (UIImageView *)gestureRecognizer.view;
 // do stuff;
NSLog(@"it works");

检查collectionview委托

它不在 didDeselectItemAtIndexPath

-(void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath 

它在 didselectItemAtIndexPath

-(void)collectionView:(UICollectionView *)collectionView didselectItemAtIndexPath:(NSIndexPath *)indexPath 

【讨论】:

【参考方案2】:

似乎在您的课程中找不到 onButtonTapped 方法,这就是它给您带来崩溃的原因。

【讨论】:

【参考方案3】:

因为函数在集合单元格中

UITapGestureRecognizer * tap=[[UITapGestureRecognizer alloc] initWithTarget:cell action:@selector(onButtonTapped:)];

【讨论】:

那么我可以删除 colcell.m 文件中的整个方法吗?? 是的。如果您在视图控制器中使用目标self,则可以在视图控制器中添加该功能

以上是关于集合视图中的点击手势无法执行的主要内容,如果未能解决你的问题,请参考以下文章

如何为集合视图单元格内的视图添加手势?

为图像添加触摸手势并执行代码

在 uiimageview 上无法识别点击手势

当从视图启用滚动时,SWRevealViewController 平移手势不起作用

Swift:手势识别器无法识别的选择器发送到实例

捏合和点击手势无法正常工作