2015-10-01 90 views
0

當我嘗試從詳細視圖控制器接收數據時,主視圖控制器未添加新的單元格。我想根據詳細視圖控制器中的數組更新主視圖控制器中的表視圖。也取消按鈕不能工作超過一次如何使用nsobject類中的數組更新tableview單元格

這是用於保存和取消按鈕的主視圖控制器代碼。

(void)detailControllerSaved:(DetailViewController *)controller { 

    if ([self.presentedViewController isEqual:controller]) { 

     [self.moviesToDisplay addObject:controller.detailItem]; 
     [self dismissViewControllerAnimated:YES 
           completion:nil]; 

     // it's a modal view 
    } else { 

     // it's a navigation view 
     NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow]; 

     self.moviesToDisplay[indexPath.row] = controller.detailItem; 
     [self.navigationController popViewControllerAnimated:YES]; 
    } 

    [self.tableView reloadData]; 
} 

(void)detailControllerCanceled:(DetailViewController *)controller { 

    if ([self.presentedViewController isEqual:controller]) { 

     // it's a modal view 
     [self dismissViewControllerAnimated:YES 
           completion:nil]; 

    } else { 

     // it's a navigation view 
     [self.navigationController popViewControllerAnimated:YES]; 
    } 
} 

This is my array list 



(void) loadSampleContent { 

    NSArray *movies = @[ 
         [Films movieWithGenre:@"Comedy" title:@"LittleMan " date:@"2006" rate:@"7.5"], 

    self.moviesToDisplay = [[NSMutableArray alloc] initWithArray:movies]; 
} 



and this is the code in the detail view controller 




(void)viewWillAppear:(BOOL)animated { 
    if(self.detailItem) { 
     self.movieTitle.text = self.detailItem.movieTitle; 
     self.genreField.text = self.detailItem.movieGenre; 
     self.movieRate.text = self.detailItem.movieRate; 
     self.yearOfMovie.text = self.detailItem.movieDate; 
    } 
} 

(void)didReceiveMemoryWarning { 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

(IBAction)Save:(id)sender { 
    self.detailItem = 
    [Films movieWithGenre:self.genreField.text title:self.movieTitle.text date:self.yearOfMovie.text rate:self.movieRate.text]; 
    [self.delegate detailControllerSaved:self]; 
} 
(IBAction)cancel:(id)sender { 
    [self.delegate detailControllerCanceled:self]; 
} 

回答

0

我想你從Detail viewcontroller收到數據後錯過了重載表。嘗試添加重裝tableview中在mastercontroller在此代碼:

如果([self.presentedViewController isEqual:方法控制器]){

[self.moviesToDisplay addObject:controller.detailItem]; 
    [self dismissViewControllerAnimated:YES 
          completion:nil]; 

    // it's a modal view 
} 

希望這有助於!

+0

我已經有此代碼。 – omar

相關問題