简体   繁体   English

从DetailViewController弹出后重新加载TableView数据

[英]Reloading TableView Data after pop From DetailViewController

How can I reload my tableView data after I click back button/pop from my detailViewController (using UINavigationController and push)? 在我的detailViewController单击后退按钮/弹出窗口(使用UINavigationController和push)后,如何重新加载tableView数据? I tried reloading my tableView in viewDidAppear but nothing has changed. 我尝试在viewDidAppear重新加载tableView,但是没有任何改变。 Below I attached my storyboard so that you guys can have a idea about what my problem is. 在下面附上我的故事板,以便你们对我的问题有一个了解。 When I click save, I want all the data to be automatically reloaded to my tableView when I go back to my UITableViewController . 当我单击保存时,当我返回UITableViewController时,我希望所有数据都自动重新加载到tableView中。 How can I achieve this? 我该如何实现?

http://i.stack.imgur.com/i1YyV.png http://i.stack.imgur.com/i1YyV.png

Below is my code for saving new data to coreData using saveButton. 以下是我使用saveButton将新数据保存到coreData的代码。

- (IBAction)saveButton:(id)sender {
    CoreDataAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
    NSManagedObjectContext *context = [appDelegate managedObjectContext];
    NSManagedObject *newContact = [NSEntityDescription insertNewObjectForEntityForName:@"Contact" inManagedObjectContext:context];

    NSString *name = self.nameTextfield.text;
    NSString *phoneNumber = self.phoneTextfield.text;

    CoreDataTableViewController *cdtvc = [[CoreDataTableViewController alloc] init];

    [newContact setValue:name forKey:@"name"];
    [cdtvc.name addObject:name];
    [newContact setValue:phoneNumber forKey:@"phone"];
    [cdtvc.phone addObject:phoneNumber];

    NSError *error;
   [context save:&error];
   [cdtvc.tableView reloadData];
}

You are reloading the tableView of another instance of your CoreDataTableViewController . 您正在重新加载CoreDataTableViewController的另一个实例的tableView With this line: 用这行:

 CoreDataTableViewController *cdtvc = [[CoreDataTableViewController alloc] init]; 

you create a new CoreDataTableViewController instance but when you pop the current view controller from the navigation stack another instance will be visible whose table view has not received the message -reloadData . 您创建了一个新的CoreDataTableViewController实例,但是当您从导航堆栈中弹出当前视图控制器时,将看到另一个实例,该实例的表视图尚未收到消息-reloadData

To fix this replace the above line with this code: 要解决此问题,请使用以下代码替换上面的行:

NSArray *navigationStack = [self.navigationController viewControllers];
NSInteger previousViewControllerIndex = navigationStack.count - 2;
CoreDataTableViewController *cdtvc = [navigationStack objectAtIndex:previousViewControllerIndex];

and see if it does the trick. 看看是否能解决问题。 (Now cdtv is a reference to the original view controller on the navigation stack.) (现在,cdtv是对导航堆栈上原始视图控制器的引用。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从对象传递数据后,TableView不会重新加载 - TableView is not reloading after data passing from object 将parse.com云后端数据从TableView传递到DetailViewController - Pass parse.com cloud backend data from TableView to DetailViewController 从iOS5中的TableView将数据传递到DetailViewController时出现问题 - Problems passing data to a DetailViewController from TableView in iOS5 在Swift中从视图控制器之间传递数据(从TableView到DetailViewController) - Passing data between View Controllers in Swift (From TableView to DetailViewController) 无法从tableview加载detailViewController - Cant load detailViewController from tableview TableView 在推送 controller 时不会重新加载数据并将其弹出? - TableView is not reloading data while pushing a controller and pop it back? 从表格视图重新加载单元格后管理表格视图单元格,文本字段中的数据不清晰 - Manage tableview cell after reloading cell from tableview, Data in textfield is not getting clear 完成解析 function 后重新加载 TableView 中的数据 - Reloading Data in TableView after completing parse function 在获取异步JSON数据后重新加载tableview - Reloading tableview after fetching asynchronous JSON data 重新加载tableview后没有获取表视图数据 - not getting table view data after reloading tableview
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM