user6898211
user6898211

Reputation: 53

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.tag=indexPath.row;
    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];
        }
    }
}

Upvotes: 0

Views: 268

Answers (3)

KKRocks
KKRocks

Reputation: 8322

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];

Upvotes: 0

Sagar Shirbhate
Sagar Shirbhate

Reputation: 821

Remove all gesture from imageView in cell for row at indexpath First

for (UIGestureRecognizer *recognizer in cellimg.gestureRecognizers) {
    [cellimg removeGestureRecognizer:recognizer];
}

and then add the gesture as you use in mentioned code. Use like this because when cell For row called the cell is reused so wrong image get selected.

Upvotes: 0

Olga Nesterenko
Olga Nesterenko

Reputation: 1334

The error is in those lines:

UIImageView *cellimg=(UIImageView*)[cell viewWithTag:101];
cellimg.tag=indexPath.row;

You are changing the image view tag, and then, when cell is being reused, it doesn't have a view with tag 101, so your cellimg is nil.

Upvotes: 1

Related Questions