Calling a New View when selecting a Row in a 'UITableView'

14,088

Solution 1

Alrighty, what we need to do is use one of the UITableView methods that are already readily available to us. We'll do the following.

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

ProteinView *detailViewController = [[ProteinView alloc] initWithNibName:@"ProteinView" bundle:nil];

        // It is here we'd pass information from the currently selected UITableViewCell to the ProteinView.
        // An example of this is the following.

        // I would do it like this, but others would differ slightly.
        NSString *titleString = [[[NSString alloc] initWithFormat:@"iPad %d",indexPath.row] autorelease];

        // title is an object of detailViewController (ProteinView). In my own instances, I have always made a NSString which in viewDiDLoad is made the self.navigationBar.title string. Look below for what my ProteinView.m and .h would look like.
        detailViewController.stringTitle = titleString;
        // ...
        // Pass the selected object to the new view controller.
        [self.navigationController pushViewController:detailViewController animated:YES];
        [detailViewController release];
}

EDIT

// -------- ProteinView.m -------- //

- (void)viewDidLoad {

[super viewDidLoad];
// Do any additional setup after loading the view from its nib.

// Here we set the navigationItem.title to the stringTitle. stringTitle is declared in the .h. Think of it as a global scope variable. It is also propertised in the .h and then synthesized in the .m of ProteinView. 
self.navigationItem.title = stringTitle;
}

I haven't compiled this, so I don't know if it'll fully work. But that is definitely the fastest and most easiest way to do it!

Solution 2

You could present the view modally like this

YourViewController2 *viewController2 = [[YourViewController2 alloc]initWithNibName:@"YourViewController2" bundle:nil];
[self presentModalViewController:viewController2 animated:YES];

Do you have more than one view to present? If so you will need to create an array with the names, pass it into the tableview and then present the correct view for the row selected based on the indexPath.row.

Share:
14,088
Paul Morris
Author by

Paul Morris

Updated on July 08, 2022

Comments

  • Paul Morris
    Paul Morris almost 2 years

    I am currently writing my first iPhone app, but have encountered an issue. I have a view which contains a UITableView. This is the first time that I have attempted this, and this is the behaviour that I am trying to achieve:

    When the user selects one of the rows, I would like this to call a new view, taking the user to a different page displaying info in reference to what they have selected.

    I have it currently, so when the user selects a row it displays a UIAlert in the same view, but this doesn;t suit my needs. I have set the UITableView up through interface builder, and inputted the following code into my .m file to set it up.

    - (NSInteger)tableView:(UITableView *)tableView 
     numberOfRowsInSection:(NSInteger)section {
        //return the value
        return 10;
    }
    
    //now we define the cells.
    - (UITableViewCell *)tableView:(UITableView *)tableView 
             cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
        // Identifier for retrieving reusable cells.
        static NSString *cellIdentifier = @"MyCellIdentifier";
    
        // Attempt to request the reusable cell.
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    
        // No cell available - create one
        if(cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
                                          reuseIdentifier:cellIdentifier];
        }
    
        // Set the text of the cell to the row index.
        cell.textLabel.text = [NSString stringWithFormat:@"iPad %d", indexPath.row];
    
        return cell;
    }
    

    This creates a list of ten rows. The following codes gives me my UIAlert when tapped, however, I want to remove this and make it call a new view of my choice;

    - (void)tableView:(UITableView *)tableView 
    didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
        // Show an alert with the index selected.
        UIAlertView *alert = [[UIAlertView alloc] 
                              initWithTitle:@"iPad Selected"                         
                              message:[NSString stringWithFormat:@"iPad %d", indexPath.row]                     
                              delegate:self       
                              cancelButtonTitle:@"OK"           
                              otherButtonTitles:nil];
        [alert show];
        [alert release];   
    
    }
    

    Can anyone help with this last piece of code? the view I want it to call is called 'ProteinView'.

  • Tim
    Tim about 13 years
    This is a correct answer, but (a) makes the presumption that OP is using a navigation view controller, and (b) doesn't really explain the different parts involved - new view controllers, XIBs, and the title attribute. Maybe elaborate a little?
  • Paul Morris
    Paul Morris about 13 years
    I have used that example, compiled etc, the UIToolBar displays fine . but when a row is tapped, the app crashes. #Dismayed!
  • Paul Morris
    Paul Morris about 13 years
    It seems to give a warning based on that [NSString withFormat: piece of code. See screenshot if anyone is still reading this :-) - d.pr/AKNb
  • Rayfleck
    Rayfleck about 13 years
    it should be [NSString stringWithFormat:@"iPad %d",indexPath.row];
  • Paul Morris
    Paul Morris about 13 years
    @Mike, thanks for the comment, well that certainly helped. The app no longer crashes upon touching a row, however it still doesnt take me to my new 'ProteinView' - it just highlights the row and leaves it highlighted. Doesnt crash, doesnt change views. Just stays on that same view with a highlighted row.
  • Rayfleck
    Rayfleck about 13 years
    One of the oldest bugs in the iPhone book - pushing a view onto a null UINavigationController. (I believe @Tim pointed that out already). You need to shoehorn a navigation controller into the view hierarchy, maybe making your tableView the rootViewController of the NavController. Then when you try to push your detail view onto it, it will exist, and be more likely to work. :-)
  • Sebastien Peek
    Sebastien Peek about 13 years
    @Mike definitely has it there. I apologise for not taking that into consideration.
  • Rayfleck
    Rayfleck about 13 years
    I don't know if I can ever forgive you. But I'll try. :-)
  • Paul Morris
    Paul Morris about 13 years
    Mike, Mike, Mike - that all sounds wonderful, however, you know that I literally have no idea what you are talking about haha. I just read your tweet, so if you get time can you sort out a small example project? ill mail you now
  • d4ndym1k3
    d4ndym1k3 about 13 years
    lol, i know it's a bit of a pain but start a new project with the tutorial i posted. i know you think it's easy to do, but it can be tricky trying to add extra navigation controllers to an existing project. if you complete the tutorial (part1&2) you'll have an outline of what you want to do, then you can just add what you know to your project, or add your project to the sample tutorial
  • Paul Morris
    Paul Morris about 13 years
    Indeed, you are right. I will give it a go this evening, may need to think of a new way to present what I want to show. Thanks all