简体   繁体   English

iOS为什么我的应用程序在重启后才将数据加载到表视图中?

[英]iOS Why won't my app load data into the tableview until it's restarted?

In my nooby quest to learn how to properly load data into a grouped table view from Core Data, I built a very simple little two-view app. 在我的nooby探索中,如何学习如何从Core Data正确地将数据加载到分组表视图中,我构建了一个非常简单的小两视图应用程序。 I use MagicalRecord as my interface with CD. 我将MagicalRecord用作CD的界面。

The current problem is that, while input data is being saved to my store as expected, the table view isn't refreshed upon saving to display the new data. 当前的问题是,尽管输入数据已按预期保存到我的商店中,但是保存后不会刷新表视图以显示新数据。 However, if I stop the app in the simulator, then restart from Xcode, the new data is displayed in the tableview. 但是,如果我在模拟器中停止该应用程序,然后从Xcode重新启动,则新数据将显示在表视图中。

I'm using NotificationCenter rather than delegation for the communiation. 我正在使用NotificationCenter而不是委派进行通信。 Here is the code from my initial View Controller (containing and delegate to the tableview), followed by the code from the AddViewController, used to add data. 这是我最初的View Controller中的代码(包含并委托给tableview),然后是AddViewController的代码,用于添加数据。 Can someone with MagicalRecord experience please shine a light on me? 有MagicalRecord经验的人可以给我照亮吗? Thanks! 谢谢!

//
//  ViewController.m
//  MRGroupingTest
//
//  Created by Tim Jones on 1/9/14.
//  Copyright (c) 2014 TDJ. All rights reserved.
//

#import "ViewController.h"
#import "ListActivity.h"

@interface ViewController ()

{
    NSFetchedResultsController *frc;
}


@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    [self refreshData];
    frc = [ListActivity MR_fetchAllGroupedBy:@"category" withPredicate:nil sortedBy:@"name" ascending:YES];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationNewActivityAdded:) name:@"newActivityAdded" object:nil];

}

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

-(void) notificationNewActivityAdded:(NSNotification*)notification
{
    [self.myTableView reloadData];
}



#pragma mark Table View data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [[frc sections] count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    id<NSFetchedResultsSectionInfo> sectionInfo = [[frc sections] objectAtIndex:section];
    int rows = [sectionInfo numberOfObjects];
    return rows;

}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    id <NSFetchedResultsSectionInfo> sectionInfo = [[frc sections] objectAtIndex:section];
    return [sectionInfo name];

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    // Configure the cell...
    if ([frc sections] > 0)
    {
        ListActivity *activity = [frc objectAtIndexPath:indexPath];
        cell.textLabel.text = activity.name;

    }

    return cell;

}

-(void) refreshData
{

    frc = [ListActivity MR_fetchAllGroupedBy:@"category" withPredicate:nil sortedBy:@"name" ascending:YES];

    [self.myTableView reloadData];
}


#pragma mark Table View delegate

@end

And here's the code from AddViewController: 这是AddViewController的代码:

//
//  AddViewController.m
//  MRGroupingTest
//
//  Created by Tim Jones on 1/9/14.
//  Copyright (c) 2014 TDJ. All rights reserved.
//

#import "AddViewController.h"

@interface AddViewController ()

@end

@implementation AddViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

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

- (IBAction)saveAction:(UIBarButtonItem *)sender
{
    NSManagedObjectContext *localContext = [NSManagedObjectContext MR_contextForCurrentThread];

    ListActivity * thisActivity = [ListActivity MR_createInContext:localContext];
    thisActivity.name =  self.activityField.text;
    thisActivity.category = self.categoryField.text;
    [localContext MR_saveToPersistentStoreAndWait];
    //Inform app
    [[NSNotificationCenter defaultCenter] postNotificationName:@"newActivityAdded" object:localContext];
    //dismiss view
    [self.navigationController dismissViewControllerAnimated:YES completion:nil];


}

- (IBAction)cancelAction:(UIBarButtonItem *)sender
{
    NSManagedObjectContext *localContext = [NSManagedObjectContext MR_contextForCurrentThread];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"newActivityAdded" object:localContext];
    //dismiss view
    [self.navigationController dismissViewControllerAnimated:YES completion:nil];

}


@end

您需要重新加载TableView数据,即调用reloadData函数

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

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