santa
santa

Reputation: 147

Cant detect button action collectionview

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!

Upvotes: 1

Views: 1156

Answers (2)

Pavan
Pavan

Reputation: 18518

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

Upvotes: 1

Oliver Atkinson
Oliver Atkinson

Reputation: 8029

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:

Upvotes: 1

Related Questions