user6898211 user6898211 - 1 year ago 85
iOS Question

when I am adding Long Press Gesture on uiimageview in Table View Cell .Uiimageview Is not showing With correct Image

I am Using the Long Press Gesture code on uiimageview the Problem is Profile Picture is not showing Correct.I have 50 values in Table View and after 5 to 6 images Further Cell image is going to be Nil.and Profile Picture is coming from Web Service.and if i will not add long press all 50 rows will display with their correct Image.
this is my code::

#pragma mark - UITableView
- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section
return arrResultData.count;
- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"FishCell" forIndexPath:indexPath];
// getting the imag which is in prototype cell in storyboard
UIImageView *cellimg=(UIImageView*)[cell viewWithTag:101];
cellimg.userInteractionEnabled = YES;
UILongPressGestureRecognizer *gestureRecognizer = [[UILongPressGestureRecognizer alloc] init];
gestureRecognizer.delegate = self;
gestureRecognizer.minimumPressDuration = 0.5;
[cellimg addGestureRecognizer: gestureRecognizer];
[gestureRecognizer addTarget:self action:@selector(imgLongPressed:)];

- (void)imgLongPressed:(UILongPressGestureRecognizer*)sender
// UIImageView *view_ =(UIImageView*) sender.view;
NSLog(@"view tag %ld",sender.view.tag);
// CGPoint point = [sender locationInView:view_.superview];
if (sender.state == UIGestureRecognizerStateBegan){
Profile_PopUP_Vc *Profile_PopUPVc = [self.storyboard instantiateViewControllerWithIdentifier:@"Profile_PopUP_Vc"];
Profile_PopUPVc.ImageUrl=[[arrResultData valueForKey:@"picture"]objectAtIndex:sender.view.tag];
Profile_PopUPVc.strUsername=[[arrResultData valueForKey:@"username"]objectAtIndex:sender.view.tag];
Profile_PopUPVc.delegate = self;
[self presentPopupViewController:Profile_PopUPVc animationType:MJPopupViewAnimationFade];
else if (sender.state == UIGestureRecognizerStateEnded){
[self dismissPopupViewControllerWithanimationType:MJPopupViewAnimationFade];

Answer Source

You need to get indexpath using view's hierarchy and use this indexpath instead of its tag value .

UIImageView *view_ =(UIImageView*) sender.view
UITableViewCell *cell = (UITableViewCell *)view_.superview.superview;
NSIndexPath *indexPath = [recipeCollectionView indexPathForCell:cell];
Profile_PopUPVc.ImageUrl=[[arrResultData valueForKey:@"picture"]objectAtIndex:indexPath.row];
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download