从UITableViews获取核心数据对象不是从Core Data制作的

时间:2015-06-28 17:02:29

标签: ios objective-c core-data nsfetchedresultscontroller

我正在尝试获取Core Data对象并从不是由Core Data对象组成的UITableView加载它。它只是用预定数组制作的UITableView,但我想在Core Data中加载一个与单元格detailTextLabel同名的对象。我尝试按照this tutorial加载特定值,但它不起作用:它崩溃或告诉我我的实体是nil,但事实并非如此。

所以基本上我有一个UITableView,它加载了这个数组:

array = @[@"publication 1",@"publication 2", etc]

然后,当用户选择单元格时,我希望它基于单元格detailText加载文档,但它总是加载对象indexPath而不是基于单元格detailText。所以说在主视图控制器中预定数组被加载到表视图中,我点击Memo 24,但它的索引是(0,2)它将加载一个对象,但它会加载下载实体中的对象具有相同的indexPath(无论出版物是否为0,2)数字,而不是备忘录24标题。

这是我的代码:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

    [self loadPublicationLocallyWithPubAtIndex:indexPath withTitle:cell.detailTextLabel.text];
}

- (void)loadPublicationLocallyWithPubAtIndex:(NSIndexPath *)indexPath withTitle:(NSString *)pubNumber {
    NSManagedObjectContext *selectedObject = [self.fetchedResultsController objectAtIndexPath:indexPath];
    NSString *phrase = nil; // Document password (for unlocking most encrypted PDF files)
    NSString *filePath;
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"PDFs"];
    filePath = [filePath stringByAppendingPathComponent:[selectedObject valueForKey:@"puburl"]]; assert(filePath != nil); ;
    NSLog(@"Local path is : %@", filePath);
    if ([[NSFileManager defaultManager] fileExistsAtPath:filePath])
    {
        //File exists
        NSData *data = [[NSData alloc] initWithContentsOfFile:filePath];
        if (data)
        { 
            //load document
        }
    }
}

它适用于所有使用Core Data填充的视图控制器,无论单元格是indexPath,它仍然会检索正确的对象,这使我相信它与{{{{{{ 1}}部分,所以我甚至尝试过:

[self.fetchedResultsController objectAtIndex:];
NSIndexPath *selectedItem = [self.fetchedResultsController indexPathForObject:cell.detailTextLabel.text]; 中的

,但它也不起作用。

如何根据单元格didSelectRowAtIndexPath:而非detailTextLabel加载对象?

1 个答案:

答案 0 :(得分:2)

如果从静态数组填充数组,则可能不需要使用获取的结果控制器。您应该只执行一次获取来获取相关的NSManagedObject。要确保获取只获取相关对象,请使用谓词。

假设您的NSManagedObjects位于Publication实体中,相关属性为title。您应该构建一个这样的提取:

NSManagedObjectContext *context = self.managedObjectContext; // or however your context is obtained
NSManagedObject *selectedObject;
NSFetchRequest *fetch = [NSFetchRequest fetchRequestWithEntityName:@"Publication"]; // amend entity name as appropriate
fetch.predicate = [NSPredicate predicateWithFormat:@"title == %@",pubNumber];
NSError *error;
NSArray *results = [context executeFetchRequest:fetch error:&error];
if (results == nil) {
    NSLog(@"Fetch error %@, %@", error, [error userInfo]);
    abort();
} else {
    if ([results count] == 0) {
        NSLog(@"Publication not found");
    } else {
        // (note, should probably also check for count > 1 and handle accordingly)
        selectedObject = results[0];
        // then carry on with the remaining code from loadPublicationLocally....
        NSString *phrase = nil; // Document password (for unlocking most encrypted PDF files)
        NSString *filePath;
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"PDFs"];
        filePath = [filePath stringByAppendingPathComponent:[selectedObject valueForKey:@"puburl"]]; assert(filePath != nil); ;
        // ... etc etc.
    }
}