1

在我的项目中,我有一种添加/显示/编辑联系人的机制,与地址簿应用程序类似。我使用核心数据进行数据管理。基本上,当应用程序加载时,我当前的所有联系人都在那里,但是当我添加一个新联系人并返回到所有联系人的表格视图时,我看不到任何变化。然后当我关闭应用程序并再次打开它时,更改是可见的。有人可以让我知道我做错了什么吗?我希望它在添加新联系人后刷新。

运动员.m

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIBarButtonItem *addButton = [[UIBarButtonItem alloc]
                                  initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addAthlete)];
    self.navigationItem.rightBarButtonItem = addButton;

    AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
    _managedObjectContext = [appDelegate managedObjectContext];

    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    NSEntityDescription *athlete = [NSEntityDescription entityForName:@"Athlete" inManagedObjectContext:_managedObjectContext];
    [request setEntity:athlete];
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"first" ascending:YES];
    NSArray *sortDescriptors = [[NSArray alloc]initWithObjects:sortDescriptor, nil];
    [request setSortDescriptors:sortDescriptors];

    NSError *error = nil;
    NSMutableArray *mutableFetchResults = [[_managedObjectContext executeFetchRequest:request error:&error] mutableCopy];
    if (mutableFetchResults == nil){
        //handle error
    }
    [self setAthleteArray:mutableFetchResults];


    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}


-(void)addAthlete{
    AthleteAdd *addAthlete= [self.storyboard instantiateViewControllerWithIdentifier:@"addAthlete"];
    [self.navigationController pushViewController:addAthlete animated:YES];
}

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

    Athlete *athlete = (Athlete *)[athleteArray objectAtIndex:indexPath.row];
    cell.textLabel.text = [athlete first];
    cell.detailTextLabel.text = [athlete last];
    // Configure the cell...

    return cell;
}

addathlete.m

- (void)viewDidLoad
{
    [super viewDidLoad];
    AppDelegate *appDelegate = [[UIApplication sharedApplication]delegate];
    _managedObjectContext = [appDelegate managedObjectContext];

    UIBarButtonItem *saveButton=[[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(saveAthlete)];
    UIBarButtonItem *cancelButton=[[UIBarButtonItem alloc] initWithTitle:@"Cancel" style:UIBarButtonItemStyleDone target:self action:@selector(cancelSave)];
    self.navigationItem.rightBarButtonItem = saveButton;
    self.navigationItem.leftBarButtonItem = cancelButton;
}

-(void)saveAthlete{
    Athlete *athlete = [NSEntityDescription insertNewObjectForEntityForName:@"Athlete" inManagedObjectContext:_managedObjectContext];
    [athlete setFirst:firstTextField.text];
    [athlete setLast:lastTextField.text];

    NSError *error = nil;
    if(![_managedObjectContext save:&error]){
        //handle dat error
    }
    [[self navigationController] popViewControllerAnimated:YES];
}

-(void)cancelSave{

    [[self navigationController] popViewControllerAnimated:YES];

}
4

2 回答 2

1

新的 Athlete 被添加到持久存储中,但您的表没有显示更新的数据。添加新的 Athlete 对象后,将更新后的数据从持久存储中提取到 AthleteArray(步骤 1)并重新加载表视图(步骤 2)。

步骤1:

AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
    _managedObjectContext = [appDelegate managedObjectContext];

    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    NSEntityDescription *athlete = [NSEntityDescription entityForName:@"Athlete" inManagedObjectContext:_managedObjectContext];
    [request setEntity:athlete];
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"first" ascending:YES];
    NSArray *sortDescriptors = [[NSArray alloc]initWithObjects:sortDescriptor, nil];
    [request setSortDescriptors:sortDescriptors];

    NSError *error = nil;
    NSMutableArray *mutableFetchResults = [[_managedObjectContext executeFetchRequest:request error:&error] mutableCopy];
    if (mutableFetchResults == nil){
        //handle error
    }
    [self setAthleteArray:mutableFetchResults];

第2步:

[self.tableView reloadData];
于 2013-08-01T04:01:57.770 回答
1

重新加载tabelViewin viewDidAppear:ORviewWillAppear:方法。方法在andviewDidLoad:时不调用。这就是为什么更新在.popdismisstableView

于 2013-08-01T04:18:33.040 回答