简体   繁体   中英

Using TapGestureRecognizer over profileimage in TableViewController (wrong indexPath)

Good morning,

I'm going to update my post because I need your help (I'm out of solutions) with my problem using the TapGestureRecognizer.

That's my Segue code:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    NSIndexPath *myIndexPath = [self.tableView indexPathForSelectedRow];

    if ([segue.identifier isEqualToString:@"segueprofile"]) {

        OtherProfileUserViewController *destViewController = segue.destinationViewController;

        NSLog(@"jsonArray ==> %@", [[_jsonArray objectAtIndex:[myIndexPath row]] valueForKey:@"id"]);

        destViewController.recipeName = [[_jsonArray objectAtIndex:[myIndexPath row]] valueForKey:@"id"];
    }

    if ([segue.identifier isEqualToString:@"segueprofile2"]) {

        OtherProfileUserViewController *destViewController = segue.destinationViewController;

        NSLog(@"jsonArray ==> %@", [[_jsonArray objectAtIndex:[myIndexPath row]] valueForKey:@"id"]);

        destViewController.recipeName = [[_jsonArray objectAtIndex:[myIndexPath row]] valueForKey:@"id"];
    }

    if ([[segue identifier] isEqualToString:@"ShowCarDetails"])
    {
        CarDetailViewController *detailViewController = [segue destinationViewController];

        NSLog(@"jsonArray ==> %@", [[_jsonArray objectAtIndex:[myIndexPath row]] valueForKey:@"id"]);

        detailViewController.carDetailModel = [[NSArray alloc]
                                               initWithObjects:
                                               [[_jsonArray objectAtIndex:[myIndexPath row]] valueForKey:@"date"],
                                               [[_jsonArray objectAtIndex:[myIndexPath row]] valueForKey:@"id"],
                                               [[_jsonArray objectAtIndex:[myIndexPath row]] valueForKey:@"imagen"],
                                               nil];
    }
}

As you can see, I have some outputs to check the ID of the user selected, but it's not working on the firsts two cases (I have always the same ID) but in the last one everything is working fine (and I think it's because the "SELECTION" of the CUSTOM ROW.

What I have to do in order to make the other two segues work with the correct information? I have connected my imageView (first segue) to a TapGestureRecognizer and this to a Segue action, the second is the same but from a UILabel.

Any help will be much appreciated.

Regards,

If you read UITableview's documentation. The indexPathForSelectedRow method returns an index path identifying the row and section of the selected row, like button's clicked and table cell selection event two different things. Like button click will not update selected row and selected section of tableview. There are multiple ways to resolve your problem. I am giving you one of simple solution.

First create weak outlet property to Like button (if you don't have) in CarTableViewCell.h file, and connect it.

@property (weak, nonatomic) IBOutlet UIButton *likeButton;

Then, update button tag in method

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)anIndexPath
    {
        self.selectedIndexPath = anIndexPath;
        static NSString *CellIdentifier = @"carTableCell";

        CarTableViewCell *cell = [tableView
                                  dequeueReusableCellWithIdentifier:CellIdentifier];

        if (cell == nil) {
            cell = [[CarTableViewCell alloc]
                    initWithStyle:UITableViewCellStyleDefault
                    reuseIdentifier:CellIdentifier];
        }
    cell.likeButton.tag = indexPath.row;
    //I skiped your code.........
    }

Final step, use tag for cell row index in button event.

- (IBAction)plusOne:(UIButton *)sender {
NSLog(@"%ld",(long)sender.tag);
}

I have faced your problem before,

I solved it as the following:

give the cell a value for its tag property. Then get the cell:

UITableViewCell *cell = (UITableViewCell *)[self.tableView viewWithTag:tagValue];

then get the NSIndexPath like this

NSIndexPath *indexPath = [self.tableView indexPathForCell: cell];

hope it will help:)

Please write this code in viewWillAppear method instead of viewDidLoad

[self fetchJson];

[self.tableView reloadData];

// Initialize the refresh control.
self.refreshControl = [[UIRefreshControl alloc] init];
//self.refreshControl.backgroundColor = [UIColor blackColor];
//self.refreshControl.tintColor = [UIColor whiteColor];
[self.refreshControl addTarget:self
                        action:@selector(fetchJson)
              forControlEvents:UIControlEventValueChanged];

Tell me after solving Problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM