为什么Core Data获取请求从自定义UITableViewCell返回为空?

时间:2015-04-27 21:05:35

标签: ios objective-c iphone xcode core-data

我在ViewController.m中有一个名为getData的方法,它在viewDidLoad中调用:

-(void)getData {
    AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];

    NSManagedObjectContext *context = [appDelegate managedObjectContext];

    NSEntityDescription *entityDesc = [NSEntityDescription entityForName:@"WorkoutHasExercise" inManagedObjectContext:context];

    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    [request setEntity:entityDesc];
    request.resultType = NSDictionaryResultType;
    request.propertiesToFetch = [NSArray arrayWithObjects:@"exerciseName", @"reps", @"sets", nil];

    NSPredicate *pred = [NSPredicate predicateWithFormat:@"(workoutName = %@)", _workoutName];
    [request setPredicate:pred];

    NSManagedObject *matches = nil;

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

    if ([objects count] == 0) {

    } else {
        [_exercises removeAllObjects];
        for (int x = 0; x < [objects count]; x++) {
            matches = objects[x];
            [_exercises addObject:[matches valueForKey:@"exerciseName"]];
            [_totalSets addObject:[matches valueForKey:@"sets"]];
            [_totalReps addObject:[matches valueForKey:@"reps"]];
            [_currentSets addObject:[NSNumber numberWithInteger:0]];
        }
    }
    [_exercisesTableView reloadData];

}

我还有一个自定义的UITableViewCell,在cellForRowAtIndexPath中启动了两个按钮:

ActiveWorkoutCell *cell = (ActiveWorkoutCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"ActiveWorkoutCell" owner:self options:nil];
cell = [nib objectAtIndex:0];

cell.increaseButton.tag = indexPath.row;
cell.decreaseButton.tag = indexPath.row;

在ActiveWorkoutCell.m中,我有两个按钮的IBAction:

- (IBAction)decreaseSets:(id)sender {
    ActiveWorkoutViewController *vc = [[ActiveWorkoutViewController alloc] init];
    [vc decreaseSets:[sender tag]];
}

- (IBAction)increaseSets:(id)sender {
    ActiveWorkoutViewController *vc = [[ActiveWorkoutViewController alloc] init];
    [vc increaseSets:[sender tag]];
}

IBActions在ViewController.m

中调用这两个方法
-(void)increaseSets:(NSInteger)row {
    [self getData];
    //There will be code here to increase the value of currentSets[row]
}

-(void)decreaseSets:(NSInteger)row {
    [self getData]
    //Code to decrease value...
}

问题:

从viewDidLoad调用getData时,它可以正常工作。 从ActiveWorkoutCell.m中的IBAction返回ViewController.m时会发生此问题。 当我在increaseSets中调用[self getData]时,获取请求返回一个空数组。这让我很困惑 - 代码在第一次调用时工作正常,但在触发自定义单元格Action后第二次调用时根本不工作。

如果有帮助,这是我的viewDidLoad:

- (void)viewDidLoad {
    [super viewDidLoad];
    _exercises = [NSMutableArray array];
    _totalSets = [NSMutableArray array];
    _currentSets = [NSMutableArray array];
    _totalReps = [NSMutableArray array];

    _titleLabel.text = _workoutName;
    _exercisesTableView.allowsSelection = NO;
    [self getData];
}

_workoutName在前一个视图控制器的prepareForSegue中被赋予一个值。

1 个答案:

答案 0 :(得分:2)

我想我发现了这个问题。您正在实例化&#34; ActivityWorkOutViewController&#34;当调用IBAction方法并且它将是一个新实例然后在那些方法中你调用[self getData]指向没有实例化变量或发生viewDidLoad的新实例,所以你的可变数组没有被分配,因此它们是空。

只需使用该类的旧实例即可获取数据。

我不确定你是如何引用这些类的。我只是对此感到困惑。但是,您可以检查分配并调用正确的类来获取数据