如何使用nsobject类中的数组更新tableview单元格

时间:2015-10-01 02:01:28

标签: ios objective-c arrays

当我尝试从详细视图控制器接收数据时,主视图控制器未添加新单元格。我想基于详细视图控制器中的数组更新主视图控制器中的表视图。取消按钮也不能多次工作

这是保存和取消按钮的主视图控制器代码。

 (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];
}

1 个答案:

答案 0 :(得分:0)

我认为您在从Detail viewcontroller接收数据后错过了重新加载表。尝试在此代码中在mastercontroller中添加reload tableview:

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

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

    // it's a modal view
} 

希望这有帮助!