0

How to identify the cell that has been clicked. Thereafter, i want to the record stored in that row and navigate and display those values another viewcontroller.

This is a storyboard application that i am using, so can you tell me how to get this done from it.

Before, i used the following method to identify the cell clicked, and then by the use of indexPath i am able to get the record that has been selected so i can navigate to the other viewcontroller.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

How to do this in a storyboard enviroment. I am using segue to navigate.

Popeye
  • 11,839
  • 9
  • 58
  • 91
Illep
  • 16,375
  • 46
  • 171
  • 302

3 Answers3

1

You want to use the sender parameter to get the cell, then use a reference to your tableView to get the indexPath. In this example I use self.tableView:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    UITableViewCell *cell = (UITableViewCell *)sender;
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
    // Get model object related to index path and pass it to next VC
}
Alex
  • 3,861
  • 5
  • 28
  • 44
0

You have two choices really.

1) In storyboard create the segue between the cell and your target view controller. This will automatically cause the segue to fire when you click the cell.

2) Do what you are doing and call the segue yourself using performSegueWithIdentifier. In this case you need to draw the segue in storyboard from the source controller to the target controller.

In both cases you will need to arm the target controller in prepareForSegue with the data the target controller is to display. Ideally make your cell have properties which are either indexes into your data model or references to the data they are views for in the table.

In approach 1) the sender will be the cell in prepareForSegue. In approach 2) the sender will be whatever you pass in the info: parameter.

Rory McKinnel
  • 7,936
  • 2
  • 17
  • 28
  • But, how can i get the content of that row and pass it to the next viewcontroller ? – Illep Mar 09 '15 at 12:23
  • You should add a property to your cell which holds an index to the data or a reference to the data. In `createCellForRowAtIndexPath`, when you create the cell, assign the property. Then in your `prepareForSegue` you can extract it. Simplest property is to assign the indexPath. You can also use `[self.tableView indexPathForSelectedRow]` in prepareForSegue assuming the row is selected during the push. – Rory McKinnel Mar 09 '15 at 13:12
-1
you have to set a segue from your firstVc to second VC 
Then do
//In your secondVc.h
@property (weak, nonatomic)  UILabel *someLabel;
//In firstViewController.m
in didselectRowAtIndexPath call
[self performSegueWithIdentifier:@"seguename" sender:nil];
and add this method
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if([[segue identifier] isEqualToString:@"segueName"]){
    NSIndexPath *indexPath = [tablename indexPathForSelectedRow];
   secondViewController *destViewController = segue.destinationViewController;
    destViewController.someLabel = [arrayName objectAtIndex:indexPath.row];
}
Robin
  • 87
  • 7