从不是由核心数据生成的 UITableViews 获取核心数据 object

Fetch Core Data object from UITableViews not made from Core Data

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

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

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

然后,当用户选择单元格时,我希望它根据单元格 detailText 加载文档,但它总是加载 objects indexPath 是什么,而不是基于单元格 detailText。所以在主视图控制器中将预定数组加载到 table 视图中,我点击备忘录 24,但它的索引是 (0,2) 它 将加载一个 object,但它会在下载实体中加载 object,该实体具有与 Memo 24 标题相同的 indexPath(任何出版物位于 0,2)编号。

这是我的代码:

- (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
        }
    }
}

它适用于我所有使用核心数据填充的视图控制器,无论 indexPath 单元格是什么,它仍然会检索到正确的 object,所以这让我相信它有一些东西与 [self.fetchedResultsController objectAtIndex:]; 部分有关,所以我什至尝试过:

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

didSelectRowAtIndexPath:中,但它也不起作用。

如何根据单元格 detailTextLabel 加载 object 而不是 indexPath

如果您从静态数组填充数组,那么您可能不需要使用获取结果控制器。相反,您应该只执行提取以获取相关的 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.
    }
}