-3

I can't seem to find how to display another 'UIViewController' view when one row is clicked. So like when the row is clicked it goes to the next view and displays more information about row.

I think I need to use something like the - (void) tableView: (UITableView *) tableView didSelectRowAtIndexPath: (NSIndexPath *) indexPath delegate. Then my second question is how do I pass the data that was in that row, like the title, to the next view controller?

Thanks

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Airglow
  • 71
  • 6
  • 1
    Yes, that's the right delegate method. Please do some research. There are countless tutorials and existing discussions on how to do this. – rmaddy Aug 04 '14 at 18:01
  • 2
    Tip: Create a new Xcode project and choose the iOS "Master-Detail Application" template. That gives you working sample code to start with. – Martin R Aug 04 '14 at 18:08

4 Answers4

1
  CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.tableView];
        NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:buttonPosition];
        UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];

        NSString *title = cell.textLabel.text;

Like @rmaddy said, Its better if you do some research, This is very basic, I would recommend you the:

http://www.raywenderlich.com/

Site for great tutorial, they really have every thing you need there.

MCMatan
  • 8,623
  • 6
  • 46
  • 85
1

Here is a hypothetical example:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
    MyCustomViewController *vc = [[MyCustomViewController alloc]initWithTitle:selectedCell.textLabel.text];
    // Assuming your UITableViewDelegate has a navigationController
    [self.navigationController presentViewController:vc animated:YES completion:nil];
}
Justin Moser
  • 2,005
  • 3
  • 22
  • 28
1

If you are using a storyboard:

.h

@interface YourTableViewController : UIViewController 
{

// you just need int selectedRow inside of @interface
int selectedRow;
}

.m

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // this method is going to give you NSIndexPath *indexPath
    selectedRow = indexPath;
    [self performSegueWithIdentifier:@"nameOfYourSegue" sender:self];
}

You might already have this method, add the code inside of it if that's the case. This method is called when a segue is about to perform.

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if([[segue identifier] isEqualToString:@"nameOfYourSegue"]){

    //Pass data from one controller to the next
    [[segue destinationViewController] setRowIndex:selectedRow];
}

}

selectedRow is going to have the index of which row was selected with 0 being the first row.

In your next view controller's class:

.h

- (void)setRowIndex:(int)rowIndex;

.m

- (void)setRowIndex:(int)rowIndex
{
     //assign your variable to rowIndex

}

Once you have this working, you can edit this code to pass an array of information.

iOSAaronDavid
  • 140
  • 15
  • Warning: Attempt to present on while a presentation is in progress! I get this when I tap a cell in the table, any ideas why? – Airglow Aug 04 '14 at 22:18
  • 1
    Hard to say, but this type of error for me comes up when trying to do too many animations at once. When I tried to dismiss 2 view controllers that had modal segues, I get this warning because it tried to animate from one view controller to the next at the same time. This is why I use navigation controller from now on because I can simply pop to any view controller. If you have trouble passing data from view controller to view controller, check out this question: http://stackoverflow.com/questions/5210535/passing-data-between-view-controllers – iOSAaronDavid Aug 05 '14 at 14:58
0

use the method:

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

you can get the cell by the index path:

  UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

the code:

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

          UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    }

after getting the cell data youc can use it answer to question 2:

you can connect in the interface builder your cell with the next vc by seque and in the method prepareforseque just pass the data to the

segue.destenationViewController.yourPoperty now in the viewdidload method:

self.title = self.property