2013-11-28 16 views
-3

我有一個表格有5個單元格,如果用戶單擊我的第二​​個單元格,例如,我想顯示另一個表格或另一個頁面。使用didSelectRowAtIndexPath

是使用

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

的方式嗎?

+0

是的,你用它來檢測單元格上的點擊。 – joao

+0

謝謝。你能給我一個簡單的例子嗎? – kpsaravan

回答

0

這裏是導航到當在所述第二小區中的用戶點擊(假設你有一個導航控制器)的另一個視圖控制器的一個示例:

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

    if(indexPath.row == 1){ 

     MyViewController * vc = [[MyViewController alloc] init]; 
     [self.navigationController pushViewController:vc animated:YES]; 

    } 

} 
0

樣品鏈接:http://www.tutorialspoint.com/ios/ios_ui_elements_tableview.htm

if (indexPath.row ==1) 

{ 

      //Second cell index path its move another class xib 

    classname *yourcontroller = [[classname alloc] initWithNibName:@"xib file name" bundle:nil]; 

    yourcontroller.modalTransitionStyle = UIModalTransitionStyleCoverVertical; 
    [self presentViewController:helpcontroller animated:YES completion:nil]; 

} 
0

如果您正在使用故事板,您可以跳過tableView:didSelectRowAtIndexPath:並使用prepareForSegue:sender:代替:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 
    if ([segue.identifier isEqualToString:@"__SEGUE_IDENTIFIER__"]) { 
     NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow]; 

     SomeOtherViewController *vc = (SomeOtherViewController *)segue.destinationViewController; 
      // assign something to vc 
    } 
} 
相關問題