ios - 为什么核心数据获取请求从自定义 UITableViewCell 返回空?

标签 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 中,我有 2 个按钮的 IBActions:

- (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] 时,提取请求返回一个空数组。这就是让我感到困惑的地方 - 代码在第一次调用时工作正常,但在触发自定义单元格操作后第二次调用时根本不起作用。

如果有帮助,这是我的 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 在之前的 View Controller 中的 prepareForSegue 中被赋予了一个值。

最佳答案

我想我找到了问题所在。当调用 IBAction 方法时,您正在实例化“ActivityWorkOutViewController”,它将是一个新实例,然后在您调用 [self getData] 的那些方法中,它指向没有变量实例化或 viewDidLoad 发生的新实例,因此您的可变数组没有分配,因此它们是空的。

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

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

关于ios - 为什么核心数据获取请求从自定义 UITableViewCell 返回空?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29905920/

相关文章:

android - 使用 SSL 证书为 iOS 9 开发时管理开发环境

ios - PopViewController 动画 :YES when tap on UIAlertView make Keyboard appear in parentVC

ios - 'UITableView'没有可见的@interface声明选择器 'registerClass:forCellReuseIdentifier:'

iphone - 从 iPhone 应用程序中启动指南针应用程序

python - 属性错误: object has no attribute 'json_args'

ios - Xcode 10 - iPhone X 黑条

iphone - 如何将服务器连接到 Xmmp 服务器

iphone - iOS - UITableView 部分标题内的 UILabel + UIActivityIndi​​catorView

iphone - UILocalNotification 崩溃

ios - 如何在swift中通过NSManagedObjectContext更新核心日期中的对象?