使用核心数据刷新表视图

时间:2014-02-24 23:44:03

标签: ios objective-c

我在插入新项目后重新加载我的核心数据时出现问题我尝试了以下但是没有任何乐趣。我想知道这是否可能是由于我调用数据的方式?我是一个非常新的人,并且已经遵循各种教程来实现这一点,但我现在已经陷入困境。我知道viewdidappear被调用,因为nslog说明了这个,但是表没有更新

`- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
     [self.tableView reloadData];

    NSLog (@"did appear");
}`

任何想法?

//  ViewController.m
//  Core Data

#import "ViewController.h"
#import "AppDelegate.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UINavigationItem *back;
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (strong, nonatomic) NSMutableArray *name;
@property (strong, nonatomic) NSMutableArray *phone;

@end

@implementation ViewController


- (void)viewDidLoad
{
    [super viewDidLoad];



    self.name = [[NSMutableArray alloc] init];
    self.phone = [[NSMutableArray alloc] init];

    AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
    NSManagedObjectContext *context = [appDelegate managedObjectContext];
    NSEntityDescription *entityDesc = [NSEntityDescription entityForName:@"Contact" inManagedObjectContext:context];
    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    [request setEntity:entityDesc];
//    NSPredicate *pred =[NSPredicate predicateWithFormat:@"(name = %@)", @"Vea Software"];
//    [request setPredicate:pred];
    NSManagedObject *matches = nil;

    NSError *error;
    NSArray *objects = [context executeFetchRequest:request
                                              error:&error];

    if ([objects count] == 0)
    {
        NSLog(@"No matches");
    }
    else
    {
        for (int i = 0; i < [objects count]; i++)
        {
            matches = objects[i];
            [self.name addObject:[matches valueForKey:@"name"]];
            [self.phone addObject:[matches valueForKey:@"phone"]];
        }
    }

}

- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
     [self.tableView reloadData];

    NSLog (@"did appear");
}


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



- (IBAction)add:(id)sender
{
    AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];

    NSManagedObjectContext *context = [appDelegate managedObjectContext];
    NSManagedObject *newContact;
    newContact = [NSEntityDescription insertNewObjectForEntityForName:@"Contact" inManagedObjectContext:context];

    addentry = self.addtextbox.text;

    [newContact setValue: addentry forKey:@"name"];
    [self.name addObject: addentry];
    [newContact setValue: @"(555) 555 - 5555" forKey:@"phone"];
    [self.phone addObject:@"(555) 555 - 5555"];
    NSError *error;
    [context save:&error];
    [self.tableView reloadData];
}

#pragma Table View

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.name.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellID = @"cellID";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    cell.textLabel.text = [self.name objectAtIndex:indexPath.row];
    return cell;
}



@end

1 个答案:

答案 0 :(得分:2)

查看本教程http://www.raywenderlich.com/999/core-data-tutorial-for-ios-how-to-use-nsfetchedresultscontroller

您需要重新考虑视图控制器的体系结构。通常,您希望:

  • 让视图控制器加载数据,然后“坐在那里”等待接收事情已更改的通知,并根据该通知重新加载。这是“UI线程”,不应被数据访问操作阻止

  • 在后台进行数据更改。现代应用程序使用Grand Central Dispatch在后台进行这些操作,但还有其他替代方案,如NSOperation,线程等。

  • NSFetchedResultsController 为您处理UI更新。让核心数据上下文管理数据整合。

这两个概念是让事情按照自己的意愿运作的关键。否则,你可能会得到一堆代码。

祝你好运!