核心数据:在表视图中滚动时,存储在数组中的NSManagedObjects变为nil

时间:2016-02-25 15:29:37

标签: ios objective-c core-data

我有一些NSManagedObjects,我正在检索并存储在一个数组中以填充表视图。当视图首次加载时,所有数据都存在,但是当我滚动数组中的NSManagedObjects时,转为nil。

我在viewDidLoad中提取这样的内容:

AppDelegate *appDelegate = [[AppDelegate alloc] init];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Tracks" inManagedObjectContext:appDelegate.managedObjectContext];
[fetchRequest setEntity:entity];

NSError *error = nil;
NSArray *fetchedObjects = [appDelegate.managedObjectContext executeFetchRequest:fetchRequest error:&error];
if (fetchedObjects == nil) {
    NSLog(@"An Error! %@",error);
}

mutArray = [[NSMutableArray alloc] initWithArray:fetchedObjects]; 

我的NSManagedObject子类名为“Tracks”。当视图加载时,我可以从mutArray中分配一个Tracks对象,但是当我滚动Tracks对象时总是为零。这是我的cellForRowAtIndexPath方法:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *simpleTableIdentifier = @"TracksCell";

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

    Tracks *track = [mutArray objectAtIndex:indexPath.row];

    cell.titleLabel.text = track.name;
    cell.addressLabel.text = [NSString stringWithFormat:@"%@, %@",track.city,track.state];

    return cell;

}

所以,为了清楚起见,这在表首次加载时有效。但是如果我向下滚动然后备份第一行的Tracks对象是nil。当我向下滚动时,任何尚未加载的行(在视图下方)都是nil。

我在这里缺少什么?

1 个答案:

答案 0 :(得分:0)

非常感谢@JanGreve在评论中发布文档,下面是需要帮助设置NSFetchedResultsController的人的答案。

您的.h文件中

添加<NSFetchedResultsControllerDelegate>

并添加属性:

@property (nonatomic, strong) NSFetchedResultsController *fetchedResultsController;
.m文件中的

@synthesize fetchedResultsController = _fetchedResultsController;

viewDidLoad:

NSError *error = nil;
if (![[self fetchedResultsController] performFetch:&error]) {
    NSLog(@"Error! %@",error);
    abort();
}

添加此方法以填充fetchedResultsController:

#pragma mark -
#pragma mark Fetched Results Controller section
-(NSFetchedResultsController *) fetchedResultsController {
    if (_fetchedResultsController != nil) {
        return _fetchedResultsController;
    }

    AppDelegate *appDelegate = [[AppDelegate alloc] init];

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Tracks" inManagedObjectContext:appDelegate.managedObjectContext];
    [fetchRequest setEntity:entity];

    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
    [fetchRequest setSortDescriptors:sortDescriptors];

    _fetchedResultsController = [[NSFetchedResultsController alloc]initWithFetchRequest:fetchRequest managedObjectContext:appDelegate.managedObjectContext sectionNameKeyPath:nil cacheName:nil];

    _fetchedResultsController.delegate = self;

    return _fetchedResultsController;
}

然后在我的cellForRowAtIndexPath:中填充你的NSManagedObject子类,如下所示:

Tracks *track = [self.fetchedResultsController objectAtIndexPath:indexPath];