简体   繁体   中英

Tap gesture in collection view not able to perform

Everything works fine until I tap on the image This is my log--

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

this is my custom cell.m file(colcell.m)

import "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.

}

This is my collection view method

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

my aim is to perform segue the image is tapped and send a data through segue but my problem is that if I tap on the image I get an error and I have all the connections and everything working 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];

and handle the action like

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

check the collectionview delegate

it is not in didDeselectItemAtIndexPath

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

it is in didselectItemAtIndexPath

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

似乎在您的类中找不到onButtonTapped方法,这就是为什么它使您崩溃的原因。

这是因为功能在收集单元中

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM