在集合视图中点按手势无法执行

时间:2017-06-14 11:38:48

标签: ios objective-c iphone uicollectionview uitapgesturerecognizer

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

  

2017-06-14 16:58:09.451 Sliders Menu [3136:252131]    - [DrinkViewController onButtonTapped:]:无法识别的选择器发送到实例0x7ff025d50cd0 2017-06-14 16:58:09.460滑块   菜单[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 !!

3 个答案:

答案 0 :(得分:2)

   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 {

答案 1 :(得分:1)

看起来像你班上找不到的onButtonTapped方法,这就是为什么它会让你崩溃。

答案 2 :(得分:1)

因为函数在集合单元格中

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