我有一个加载了记录的表格视图,请参见下面的代码:
- (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];
仍然无法正常工作,还是有问题。