2015-02-10 43 views
0

我有一個帶有課程的表格視圖,想要在詳細視圖控制器中傳遞課程名稱。但它總是通過第一行。prepareForSegue沒有在表格中傳遞適當的行查看

- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{ 
    UITableViewRowAction *edit = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"edit" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath){ 
     [self performSegueWithIdentifier:@"editLesson" sender:nil]; 
    }]; 



- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 
    if ([[segue identifier] isEqualToString:@"editLesson"]) { 
     NSManagedObject *selectedLesson = [self.lessons objectAtIndex:[[self.tableView indexPathForSelectedRow] row]]; 
     EditLessonViewController *destViewController = segue.destinationViewController; 
     destViewController.lesson = selectedLesson; 
    } 
} 

回答

2

您應該在performSegue中傳遞indexPath作爲發送者。 還有你的代碼:

- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{ 
    UITableViewRowAction *edit = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"edit" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath){ 
     [self performSegueWithIdentifier:@"editLesson" sender:indexPath]; 
    }]; 



- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 
    if ([[segue identifier] isEqualToString:@"editLesson"]) { 
     NSIndexPath* indexPath = (NSIndexPath*)sender; 
     NSManagedObject *selectedLesson = [self.lessons objectAtIndex:indexPath.row]; 
     EditLessonViewController *destViewController = segue.destinationViewController; 
     destViewController.lesson = selectedLesson; 
    } 
} 
+0

我只是想說使用iVars,但這是整潔。你不能依賴table.indexPathForSelectedRow來獲取正在編輯的行。 – 2015-02-10 16:17:06

+0

幫助,非常感謝! – ermolushka 2015-02-10 16:40:41