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 は実行されないため、私のプログラムはここに到達しません...何か間違ったことをしていますか?

次の 2 つのメソッドを追加しました。

- (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

0

ナビゲーションコントローラーを使用している場合、ビューをリロードする正しい方法は、このようなものを呼び出すことだと思います

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

reloadData変更したい内容を設定する独自のカスタム メソッドを作成し、そのメソッドを呼び出すこともできます。

于 2013-06-11T19:23:41.887 に答える
-1

指摘したように、viewWillAppear はanimated引数を取ります。ただし、それを追加しても、View Controller をナビゲーション スタックからポップしても、への呼び出しはトリガーされませんviewWillAppear:。テーブル ビューをリロードするには、 とメソッドを使用する必要がありUINavigationControllerDelegateます。navigationController:willShowViewController:animated:navigationController:didShowViewController:animated:

于 2013-06-11T19:30:31.233 に答える