codebot
codebot

Reputation: 2646

How to identify the which button clicked on which section UICollectionView iOS?

I have an UICollectionView and it has 2 sections. Each section header has a button. I tried to send the section id to an another view controller on prepareForSegue like following.

if ([[segue identifier] isEqualToString:@"ManualCapture"]){
    NSIndexPath *indexPath = [self.collectionView indexPathForCell:sender];

    ManualCapture *vc = [segue destinationViewController];
    vc.sectionId = indexPath.section;
}

but it sends 0 as the section id. Is there any possible way to gets the section id ? How can I send the sectionId according to the button click. Basically I want to do, When I clicked the button in section 1, on prepareForSegue it should be sent 0, If I clicked the button in section 2, on prepareForSegue it should be sent 1. How can I do this?

Thanks in Advance!

Upvotes: 1

Views: 1148

Answers (1)

Akshay Sunderwani
Akshay Sunderwani

Reputation: 12476

You can do it like this: in collection view delegate method

- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath{
NSString *tagStr = [NSString stringWithFormat:@"%d%d",indexPath.section,indexPath.row];
yourButton.tag = [tagStr intValue];
}

then when you want to check for which section or index it is access it by its tag, like this:

UIButton *temp = (UIButton *)sender;
NSString *tempStr = [NSString stringWithFormat:@"%d",temp.tag];

then compare it.

Upvotes: 3

Related Questions