Reputation: 179
hi I want to get the value of index path to get the data from array of the tapped cell how do I do that?
below Is my collection view method the self.iname is array that contains name and cat is category id I have to send the selected image category id to next page I am not able to extract it
- (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];
// UITapGestureRecognizer *frameTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(frameTapGesture:)];
// [cell.dimg addGestureRecognizer:frameTapGesture];
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];
NSLog(@"%@",[self.iname objectAtIndex:indexPath.row]);
}];
}
if(cell)
{
// self.catid=[self.categoryid objectAtIndex:indexPath.row];
//NSLog(@"%@",self.catid);
}
//if (cell.selected) {
// cell.backgroundColor = [UIColor blueColor]; // highlight selection
// }
// else
// {
// cell.backgroundColor = [UIColor redColor]; // Default color
// }
return cell;
}
this is my tap gesture method
- (void)onButtonTapped:(UITapGestureRecognizer *)gestureRecognizer {
//UIImageView *myImage = (UIImageView *)gestureRecognizer.view;
// do stuff;
self.catid=self.categoryid;
NSLog(@"%@",self.catid);
CGPoint tapPoint = [gestureRecognizer locationInView:gestureRecognizer.view];
UIView *tappedView = [gestureRecognizer.view hitTest:tapPoint withEvent:nil];
if ([tappedView isKindOfClass:[UILabel class]]) {
NSLog(@"Found");
}
[self performSegueWithIdentifier:@"d2" sender:self];
NSLog(@"it works");
}
Upvotes: 0
Views: 1413
Reputation: 151
Why not to use, UICollectionView own delegate for this?
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath;
As of you are adding UITapGestureRecognizer only for 1 tap gesture
Upvotes: 0
Reputation: 1668
Try this,
- (void)onButtonTapped:(UITapGestureRecognizer *)gestureRecognizer {
NSIndexPath *indexPath = [self.collectionView indexPathForItemAtPoint:[self.collectionView convertPoint:gestureRecognizer.view.center fromView:gestureRecognizer.view.superview]];
NSLog(@"%@",indexPath);
}
Upvotes: 0
Reputation: 8322
Try this :
CGPoint position = [gestureRecognizer.view convertPoint:CGPointZero
toView:self.collectionView];
NSIndexPath *tappedIndexPath = [self.collectionView indexPathForItemAtPoint:position];
OR
NSIndexPath *indexPath = [self.collectionView indexPathForItemAtPoint: tapPoint];
Upvotes: 1