ios - 收藏 View 中的点击手势无法执行

标签 ios objective-c iphone uicollectionview uitapgesturerecognizer

一切正常,直到我点击图像 这是我的日志--

2017-06-14 16:58:09.451 Sliders Menu[3136:252131] -[DrinkViewController onButtonTapped:]: unrecognized selector sent to instance 0x7ff025d50cd0 2017-06-14 16:58:09.460 Sliders Menu[3136:252131] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[DrinkViewController onButtonTapped:]: unrecognized selector sent to instance 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.

}

这是我的 Collection View 方法

- (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!!

最佳答案

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

并像这样处理 Action

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

检查 collectionview 委托(delegate)

它不在 didDeselectItemAtIndexPath

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

它在 didselectItemAtIndexPath

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

关于ios - 收藏 View 中的点击手势无法执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44543730/

相关文章:

iphone - 没有服务器的 iOS 在线游戏?

iphone - iOS应用保护

ios - remoteIO音频单元播放回调中的AudioBufferList内容

ios - UIGestureRecognizer Delegate 方法永远不会被调用

ios - (iOS MPMusicPlayerNotification) app在后台时如何接收通知

iphone - 如何使 UIWebView 的内容适应 iPhone 屏幕大小?

iphone - 创建自定义 UIActivityIndi​​catorView

objective-c - Xcode4 核心数据模型编辑器显示问题

ios - Firestore- 在 Tableview 中显示数据

iPhone:通过 XIB 完成按钮操作来放弃键盘