0

我有一个加载了记录的表格视图,请参见下面的代码:

- (void)viewDidLoad
{
    self.navigationItem.title=@"Accounts";
//    NSString *accountFile = [[NSBundle mainBundle] pathForResource:@"Accounts2" ofType:@"plist"];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentPath = [paths objectAtIndex:0];
    NSString *plistPath = [documentPath stringByAppendingPathComponent:@"Accounts2.plist"];
    accounts = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath];
    NSLog(@"Accounts contains : %@", accounts);
    account = [accounts objectForKey:@"Account"];
    NSLog(@"account %@", account);
    number = [accounts objectForKey:@"Number"];
    dueDay = [accounts objectForKey:@"DayDue"];
    minAmount = [accounts objectForKey:@"MinAmount"];
    balance = [accounts objectForKey:@"Balance"];

    NSLog(@"data loaded"); 

    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.navigationItem.leftBarButtonItem = self.editButtonItem;

    UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(insertNewObject:)];
    self.navigationItem.rightBarButtonItem = addButton;
}

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

- (void)insertNewObject:(id)sender
{
//    if (!_objects) {
//        _objects = [[NSMutableArray alloc] init];
 //   }
 //   [_objects insertObject:[NSDate date] atIndex:0];
 //   NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
 //   [self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    if (!self.addView) {
        self.addView = [[AddView alloc] initWithNibName:@"AddView" bundle:nil];
    }
          [self.navigationController pushViewController:self.addView animated:YES];
    NSLog(@"I am done, now what");
}

#pragma mark - Table View

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSLog(@"Number of records is %d", [account count]);
    return [account count];
}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }


     NSDate *object = _objects[indexPath.row];
     cell.textLabel.text = [object description];

    NSString *nameOfAccount = [account objectAtIndex:indexPath.row];
    cell.textLabel.text = nameOfAccount;
    NSString *accountNumber = [number objectAtIndex:indexPath.row];
    cell.detailTextLabel.text = accountNumber;
    NSLog(@"Index %d", indexPath.row);
    return cell;
}

我想向表中添加一条记录,所以我使用“InsertNewObject”方法...出现新屏幕,我可以将记录添加到我的 plist,然后我在 DetailView 中执行以下行以返回:

[self.navigationController popToRootViewControllerAnimated:NO];

现在,我想用我的新记录重新加载我的 TableView:我想我只需要使用下面的 viewWillAppear 方法,如下所示:

- (void) viewWillAppear {
    NSLog(@"View will appear");
    [super viewWillAppear:YES];
    [self.tableView reloadData];

}

但是,我的 NSLog 永远不会执行,因此我的程序永远不会到达这里......我做错了什么吗,谢谢你的任何回应

我添加了以下两种方法:

- (void) navigationController:(UINavigationController *) navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated{

    [self.tableView reloadData];

}

-(void) navigationController:(UINavigationController *) navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated{
    [self.tableView reloadData];
}

并改变了线路

[self.navigationController popToRootViewControllerAnimated:YES];

仍然无法正常工作,还是有问题。

4

3 回答 3

2

您没有使用正确的方法。您忽略了animated论点,请尝试使用-(void)viewWillAppear:(BOOL)animated

UIViewController——viewWillAppear:

- (void)viewWillAppear:(BOOL)animated {
    NSLog(@"View will appear");
    [self.tableView reloadData];
    [super viewWillAppear:animated];
}
于 2013-06-11T19:28:04.933 回答
0

如果您使用的是导航控制器,我相信重新加载视图的正确方法是调用这样的东西

  -(void) viewWillAppear: (BOOL) animated 
 { 
   [self.tableView reloadData];
 }

您还可以创建自己的自定义reloadData方法来设置您想要更改的内容,然后调用该方法。

于 2013-06-11T19:23:41.887 回答
-1

正如所指出的, viewWillAppear 需要一个animated参数。但是,即使添加了它,从导航堆栈中弹出视图控制器也不会触发对viewWillAppear:. 您需要使用UINavigationControllerDelegate'snavigationController:willShowViewController:animated:navigationController:didShowViewController:animated:方法来重新加载表格视图。

于 2013-06-11T19:30:31.233 回答