1

I have a collectionview[not custom collectionview], and i would like to show an alert ,by clicking the button. But button action doesnt detect the action that is my code:

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

        UICollectionViewCell *cell = [ self.collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];

        NSArray *sectionArr=[self.ClassesArr objectAtIndex:indexPath.section];

        NSDictionary *data =  [sectionArr objectAtIndex:indexPath.row];
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(54, 25, 15, 15);
        [btn addTarget:self action:@selector(subCateBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        [cell addSubview:btn];

    }


// the button action
    -(IBAction)subCateBtnAction:(UIButton *)btn
    {
        NSArray *sectionArr=[self.ClassesArr objectAtIndex:sectionindex];

        NSDictionary *data =  [sectionArr objectAtIndex:btn.tag];
        NSString *name = [data objectForKey:@"nombreTarifa"];
        UIAlertView *Notpermitted=[[UIAlertView alloc] initWithTitle:@""
                                                             message:name
                                                            delegate:nil
                                                   cancelButtonTitle:@"OK"
                                                   otherButtonTitles:nil];
        [Notpermitted show];

    }

What can be the problem thanks!

santa
  • 147
  • 1
  • 4
  • 16
  • http://stackoverflow.com/questions/13757762/getting-button-action-uicollectionview-cell Maybe this will help you – NeoJiang Sep 22 '14 at 01:35

2 Answers2

1

Doesn't look like you have added the button to the cell or returned the cell, try this:

[cell addSubview:btn];
return cell;

BUT do you realise UICollectionView has a delegate method for selection? collectionView:didSelectItemAtIndexPath:

Oliver Atkinson
  • 7,970
  • 32
  • 43
  • Yeah . but the problem when I add collectionView:didSelectItemAtIndexPath and i click button the app is crashing? another detail is that the collection view is one subview. – santa Jan 19 '14 at 21:00
1

woah, you never want to add a button like this in your cellForItemAtIndexPath: delegate method, EVER! create a custom cell view and have the button added in there, then create instances of your button in that code

Then check that your button's selector method is being called first. You can add an NSLog statement in it to test the method is being called. Also you should be using the DidSelectItem delegate method to detect touches. You mentioned that your code was crashing when you tried to implement the didSelectItem delegate method, thats not an excuse to avoid fixing the problem and implementing your selection code elsewhere. Fix your crash and implement it properly dude. Let us know what the crash is actually about when you try to implement the didSelectItem delegate method so that we can help you further.

Update 1 Recreate your nib and need the answer in the following post. It refers to your cell being a hi view instead of a UICOllectionView which is most likely the reason why your content view is not receiving touch events

Community
  • 1
  • 1
Pavan
  • 17,840
  • 8
  • 59
  • 100
  • 1
    It has become apparent to me the OP has little knowledge of Obj-C and should brush up. There is tons of errors in the code provided. Take this mans advise and *"Fix your crash and implement it properly dude"* – Oliver Atkinson Jan 19 '14 at 21:44
  • CollectionCell *cell = [ self.collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath]; NSArray *sectionArr=[self.ClassesArr objectAtIndex:indexPath.section]; NSDictionary *data = [sectionArr objectAtIndex:indexPath.row]; [cell.img addTarget:self action:@selector(subCateBtnAction:) forControlEvents:UIControlEventTouchUpInside]; return cell; } in collectionView:cellForItemAtIndexPath ,i did like this but i couldnt see any information in my collectionview. – santa Jan 19 '14 at 22:34
  • [self.collectionView registerClass:[CollectionCell class] forCellWithReuseIdentifier:@"CollectionCell"]; and also in viewdidload , I registered the custom cell but no result:S – santa Jan 19 '14 at 22:36