How can I move to another view controller when the user clicks on a row?

前端 未结 9 1292
梦如初夏
梦如初夏 2020-12-21 17:41

I am new to iPhone development, and I want to move to another page when the user clicks on a particular row. So, if they click on first row, I want the page to redirect to t

相关标签:
9条回答
  • 2020-12-21 18:12

    first of all you don't need to set different view controller for each row (unless you have a very good reason for doing that).

    the correct way is to set 1 view controller that will fit all the cells in the raws and change its data according to the selected row.

    the way you do that is:

    in the - DidSelectRowAtIndexPath function in your implementation file you shuld:
    
        - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
         {
    
    //allocate your view controller
             DetailedViewController *detailedViewController = [[DetailedViewController alloc] init];
    
     //send properties to your view controller
             detailedViewController.property1 = someProperty1;
             detailedViewController.property2 = someProperty2;
    
     //push it to the navigationController
             [[self navigationController] pushViewController:detailedViewController animated:YES];
             [detailedViewController release];
        }
    

    i do recommend that you will start by using apple examples, they are great and there are a lot of them :

    http://developer.apple.com/library/ios/#samplecode/TableViewSuite/Introduction/Intro.html%23//apple_ref/doc/uid/DTS40007318

    Good luck

    0 讨论(0)
  • 2020-12-21 18:14

    Dig into the docs:

    http://developer.apple.com/library/ios/#DOCUMENTATION/UserExperience/Conceptual/TableView_iPhone/ManageSelections/ManageSelections.html#//apple_ref/doc/uid/TP40007451-CH9-SW6

    0 讨论(0)
  • 2020-12-21 18:16

    You can move to another page when user tap on row by simply making segue by "Control Drag" from cell to ViewController and make segue type "Push".

    0 讨论(0)
  • 2020-12-21 18:17
        - (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
              switch (indexPath.row) {
        case "X"
            // Allocate the viewController 
            [navigationcontroller.pushViewController:"yourviewcontroller" animated:YES];
            [release "yourviewcontroller"];
            break;
    
        default:
            break;
    }
    }
    

    EDIT: Didn't realize of what you're trying. As shani said, you should not create a viewcontroller for each row if there is no need to. Try changing the datasource for each row but using the same viewcontroller. So forget this implementation.

    0 讨论(0)
  • 2020-12-21 18:18

    It sounds like using a UINavigationController is what you want.

    You then push your "first view" on to the navigation controller's stack from the table view, e.g., from you UITableView delegate:

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        ...
    
        // indexPath.row == 0
        FirstViewController* firstVC = [[FirstViewController alloc] initWithNibName...];
        [self.navigationController firstVC animated:YES];
        [firstVC release];
    
        ...
    }
    
    0 讨论(0)
  • 2020-12-21 18:20

    Easy:

    Create and push your view controller in this delegate function:

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    }
    
    0 讨论(0)
提交回复
热议问题