' performFetch'从UITableView

时间:2016-11-14 08:48:37

标签: ios objective-c uitableview core-data nsfetchedresultscontroller

我有UITableVIew,其中包含人员列表。现在当我点击特定单元格时,我push一个新的视图控制器,显示与该人相关的详细信息。现在问题是这个机制适用于除此之外的所有其他UITableVIew's。当我点击一个单元格时,信息会显示出来,但它是一个完全不同的单元格。比如说,如果我点击第一个单元格,显示的详细信息是针对最后一个单元格的。对于所有其他单元格都会发生这种情况,信息显示错误或空白。我很难弄清楚它为什么适用于所有UITableVIew's,而不仅仅是这个。任何帮助将不胜感激。谢谢。

编辑

- (NSFetchedResultsController *)fetchedResultsController
{
    if (_fetchedResultsController != nil)
    {
        return _fetchedResultsController;
    }

AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
self.managedObjectContext = delegate.managedObjectContext;

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setFetchBatchSize:20];
NSSortDescriptor *sortDescriptor;
NSEntityDescription *entity;

if ([activitytype isEqualToString:@"activitytask"])
{
    entity = [NSEntityDescription entityForName:@"TaskActivity" inManagedObjectContext:self.managedObjectContext];
    sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"due_on" ascending:NO];
}
else if ([activitytype isEqualToString:@"activitychat"])
{
    entity = [NSEntityDescription entityForName:@"ChatMessageActivity" inManagedObjectContext:self.managedObjectContext];
    sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"_actdt" ascending:NO];
}
else if ([activitytype isEqualToString:@"activitycalendar"])
{
    entity = [NSEntityDescription entityForName:@"CalendarActivity" inManagedObjectContext:self.managedObjectContext];
    sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"_actdt" ascending:NO];
}

[fetchRequest setEntity:entity];

NSArray *sortDescriptors = [NSArray arrayWithObjects:sortDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];

NSPredicate *predicate;
predicate = nil;
[fetchRequest setPredicate:predicate];

NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:nil];
aFetchedResultsController.delegate = (id)self;
_fetchedResultsController = aFetchedResultsController;

NSError *error;
if (![_fetchedResultsController performFetch:&error])
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"
                                                    message:@"Core Data Error. Could not save data to device.Please contact..."
                                                   delegate:nil
                                          cancelButtonTitle:@"OK"
                                          otherButtonTitles:nil];
    [alert show];
}
NSLog(@"%@", _fetchedResultsController.fetchedObjects);
return _fetchedResultsController;
}

0 个答案:

没有答案