简体   繁体   English

UITableView不会重新加载数据

[英]UITableView doesn't reload data

I'm recently working on an app which allows users add items into the order list, and they can press a button to call out a popover table view, and I'm using core data to store the data. 我最近正在开发一个应用程序,该应用程序允许用户将项目添加到订单列表中,并且他们可以按一个按钮以调出弹出表视图,并且我正在使用核心数据来存储数据。

The app has a main table view, and each row will brings up another table view, just like the Facebook app that has a sliding menu. 该应用程序具有主表格视图,每一行都将调出另一个表格视图,就像Facebook应用程序具有滑动菜单一样。

The layout of the app looks like the following: 该应用程序的布局如下所示:

|---------------------------------------------------------------|
|                                                ______________ |
|                                               |Popover Button||
|                                                -------------- |
|---------------------------------------------------------------|
|-----------------------------|First table item 1               |
|Row to call the First table  |                                 |
|-----------------------------|---------------------------------|
|Row to call the Second table |First table item 2               |
|-----------------------------|                                 |
|Row to call the Third table  |---------------------------------|
|-----------------------------|First table item 3               |
|Row to call the Fourth table |                                 |
|-----------------------------|---------------------------------|

The problem is occurred in the following scenario: 在以下情况下会发生此问题:

  1. Launch the app. 启动应用程序。

  2. Select the first table view. 选择第一个表格视图。

  3. Pressed the add button to add some items into the list. 按下添加按钮将一些项目添加到列表中。

  4. Press the popover button to see the popover table view (the list has been updated). 按下弹出按钮以查看弹出表视图(列表已更新)。

  5. Close the popover table view. 关闭弹出菜单视图。

  6. Further add some more items into the list. 进一步将更多项目添加到列表中。

  7. Press the popover button (the list stay the same, the table view hasn't been updated). 按下弹出按钮(列表保持不变,表格视图尚未更新)。

  8. Go to another table view (or call the same table view again) 转到另一个表视图(或再次调用同一表视图)

  9. Press the popover button again (the list has then been updated). 再按一次弹出按钮(列表已更新)。

Could anyone help me to sort it out? 有人可以帮我解决吗? Thanks! 谢谢!

Below is the code: 下面是代码:

OrderView.m OrderView.m

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.navigationItem.leftBarButtonItem = self.editButtonItem;

    //load lists
    NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"Dish"];
    fetchRequest.sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"created" ascending:YES]];

    self.lists = [self.managedObjectContext executeFetchRequest:fetchRequest error:nil];

    [self.tableView reloadData];
}

- (NSManagedObjectContext *)managedObjectContext
{
    return [(AppDelegate *) [[UIApplication sharedApplication] delegate] managedObjectContext];
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
    // Delete the row from the data source

        [self.managedObjectContext deleteObject:[self.lists objectAtIndex:indexPath.row]];
        [self.managedObjectContext save:nil];

        NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"Dish"];
        fetchRequest.sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"created" ascending:YES]];

        self.lists = [self.managedObjectContext executeFetchRequest:fetchRequest error:nil];

        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}

DetailView.m DetailView.m

- (IBAction)popupAddToOrderButtonPressed:(id)sender
{
    OrderViewController *orderView = [[OrderViewController alloc] initWithNibName:@"OrderViewController" bundle:nil];
    Dish *newList = [NSEntityDescription insertNewObjectForEntityForName:@"Dish" inManagedObjectContext:orderView.managedObjectContext];
    newList.created = [NSDate date];
    newList.title = self.popupTitle.text;
    [orderView.managedObjectContext save:nil];
    orderView.lists = [orderView.lists arrayByAddingObject:newList];
}

I used NSNotification to solve this issue as the NSNotification will go through the entire app, so I set a NSNotification receiver at the popover table view, and when the popover table view receives the notification, it will do the fetch request to get data. 我使用NSNotification来解决此问题,因为NSNotification将遍历整个应用程序,因此我在弹出表视图上设置了NSNotification接收器,当弹出表视图接收到通知时,它将执行获取数据的获取请求。

And when the add button is pressed, it will send out the notification to ask the popover view to get the data. 当按下添加按钮时,它将发出通知,要求弹出视图获取数据。

So that's it, issue solved. 就是这样,问题解决了。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM