1

I have a custom location cell in my tableview with a button. When I press the button I want it to segue to the beer detail for that location. I'm using storyboards and I can get it to work by connecting the the entire cell to the BeerTableViewController with the following code

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

    if([[segue identifier] isEqualToString:@"beer"]){
        NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
        BeerTableViewController *beer = (BeerTableViewController *)[segue destinationViewController];
        Location *location = [self.location_results objectAtIndex:indexPath.row];
        beer.location_id = location.location_id;
        beer.location_name = location.location_name;
        }

}

but when I create a segue from the button instead of the entire cell it always segues with the first row no matter which row is clicked. Obviously, I'm not passing passing in the row with the button press. I've looked at a number of solutions on here without finding a clear answer. Any ideas?

Edit 1

I'm trying the following code in my TableViewController

-(void)beerTapsPressed:(id)sender {
    UITableViewCell *clickedCell = (UITableViewCell *)[[sender superview] superview];
    NSIndexPath *clickedButtonPath = [self.tableView indexPathForCell:clickedCell];
    [self performSegueWithIdentifier:@"beer" sender:clickedButtonPath];

}

I have ctrl + dragged a segue in storyboard from the TableView to the DetailView and named the segue "beer" and I have a button in the cell called "beerTaps" but I am still only getting row 1 details no matter which row I click.

2 Answers 2

4

This should work if you have the segue hooked up to the button:

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if([[segue identifier] isEqualToString:@"beer"]) {
        UITableViewCell *clickedCell = (UITableViewCell *)[[sender superview] superview];
        NSIndexPath *clickedButtonPath = [self.tableView indexPathForCell:clickedCell];
        BeerTableViewController *beer = (BeerTableViewController *)[segue destinationViewController];
        Location *location = [self.location_results objectAtIndex:clickedButtonPath.row];
        beer.location_id = location.location_id;
        beer.location_name = location.location_name;
    }
}

It's generally better not to use the button's position in the view hierarchy to get the cell though. A better approach would be to give the button a tag in cellForRowAtIndexPath that's equal to indexPath.row. Then in prepareForSegue, you can just do this:

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(UIButton *)sender {
    if([[segue identifier] isEqualToString:@"beer"]) {
        BeerTableViewController *beer = (BeerTableViewController *)[segue destinationViewController];
        Location *location = [self.location_results objectAtIndex:sender.tag];
        beer.location_id = location.location_id;
        beer.location_name = location.location_name;
    }
}
1
  • I ended up giving the button a tag and using your suggested method. Thanks!
    – jacobt
    Apr 26, 2013 at 3:08
2

You could wire up the segue between your tableview controller and the target controller, then you could call performSegueWithIdentifier when someone taps the button. As far as communicating the indexPath of the cell in which the button was tapped, you could use something like this:

-(void)buttonPressed:(id)sender {
 UITableViewCell *clickedCell = (UITableViewCell *)[[sender superview] superview];
 NSIndexPath *clickedButtonPath = [self.tableView indexPathForCell:clickedCell];


}

You could also set the tag property of your buttons to know which one, but that gets ugly if you have more than one section in your table. If you do use that method, you'll want to be sure to reset the tag when the cell is reused too...

2
  • Thanks, I am trying your solution but still coming up short. I edited my question to reflect my attempt. Any thoughts?
    – jacobt
    Apr 25, 2013 at 23:03
  • This worked for me. In my case i had to use it like [[[sender superview]superview]superview] cuz i have another UIView layer inside my contentView of UITableViewCell.
    – EFE
    Oct 8, 2015 at 11:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.