tableviewcontroller 看不到 configureCell:atIndexPath (使用 restkit 的核心数据)

Posted

技术标签:

【中文标题】tableviewcontroller 看不到 configureCell:atIndexPath (使用 restkit 的核心数据)【英文标题】:tableviewcontroller does not see the configureCell:atIndexPath (core data with restkit) 【发布时间】:2013-10-16 12:27:30 【问题描述】:

我使用my own JSON data 关注了restkit 的overview tutorial。

如教程中所述,我正在使用主表视图样板。根据我的实现,我能够在控制台中显示映射,但我无法在我的表视图单元格中将正确答案显示为文本标签。我很感激,如果有人可以向我解释为什么我没有得到单元格标签中的文本。

这是我为单元格标签文本指定的地方

- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath

    NSManagedObject *object = [self.fetchedResultsController objectAtIndexPath:indexPath];
    cell.textLabel.text = [[object valueForKey:@"CorrectAnswer"] description];
    NSLog(@"This is what i am looking for: %@", [object valueForKey:@"CorrectAnswer"]);


这是我的控制台结果

2013-10-16 05:07:31.963 Quizi[27422:a0b] I restkit:RKLog.m:34 RestKit logging initialized...
2013-10-16 05:07:32.281 Quizi[27422:a0b] T restkit.network:RKObjectRequestOperation.m:178 GET 'https://xxx.xxx./':
request.headers=(null)
request.body=(null)
2013-10-16 05:07:32.777 Quizi[27422:1003] T restkit.network:RKResponseMapperOperation.m:451 Mapping HTTP response to nil target object...
2013-10-16 05:07:32.781 Quizi[27422:1003] W restkit.core_data:RKManagedObjectMappingOperationDataSource.m:243 Performing managed object mapping with a nil managed object cache:
Unable to update existing object instances by identification attributes. Duplicate objects may be created.
2013-10-16 05:07:32.783 Quizi[27422:1003] W restkit.object_mapping:RKMapperOperation.m:99 Adding mapping error: No mappable values found for any of the attributes or relationship mappings
2013-10-16 05:07:32.788 Quizi[27422:3407] T restkit.network:RKObjectRequestOperation.m:248 GET 'https://xxx.xxx/22' (200 OK / 0 objects) [request=0.4945s mapping=0.0117s total=0.5176s]:
response.headers=
    "Cache-Control" = "max-age=100";
    "Content-Encoding" = gzip;
    "Content-Type" = "application/json; charset=utf-8";
    "DWAS-Handler-Name" = "System.Web.Http.WebHost.HttpControllerHandler";
    Date = "Wed, 16 Oct 2013 12:07:34 GMT";
    Etag = "\"dbc1a97b-e968-4a2a-a697-77328a8d5b94\"";
    Server = "Microsoft-IIS/8.0";
    "Set-Cookie" = "ARRAffinity=ba0ce9e0d0d8ad0cd8b963604bd90760f5428ad8490fed51bd3786d0fe9002e3;Path=/;Domain=somedomain, WAWebSiteSID=a474e52a3f1f49f48da74b52968824b9; Path=/; HttpOnly";
    "Transfer-Encoding" = Identity;
    Vary = "Accept-Encoding";
    "X-AspNet-Version" = "4.0.30319";
    "X-Powered-By" = "ASP.NET, ARR/2.5, ASP.NET";

response.body="some response body here"

这是我在主表视图中的获取结果控制器

- (NSFetchedResultsController *)fetchedResultsController

    if (_fetchedResultsController != nil) 
        return _fetchedResultsController;
    

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    // Edit the entity name as appropriate.
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Items" inManagedObjectContext:self.managedObjectContext];
    [fetchRequest setEntity:entity];

    // Set the batch size to a suitable number.
    [fetchRequest setFetchBatchSize:20];

    // Edit the sort key as appropriate.
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"CorrectAnswer" ascending:NO];
    NSArray *sortDescriptors = @[sortDescriptor];
    [fetchRequest setSortDescriptors:sortDescriptors];

    // Edit the section name key path and cache name if appropriate.
    // nil for section name key path means "no sections".
    NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:@"Master"];
    aFetchedResultsController.delegate = self;
    self.fetchedResultsController = aFetchedResultsController;

    NSError *error = nil;
    if (![self.fetchedResultsController performFetch:&error]) 
         // Replace this implementation with code to handle the error appropriately.
         // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. 
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    

    return _fetchedResultsController;
    

这就是我的映射方式

- (void)viewDidLoad

    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.navigationItem.leftBarButtonItem = self.editButtonItem;

    UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(insertNewObject:)];
    self.navigationItem.rightBarButtonItem = addButton;
      RKManagedObjectStore *managedObjectStore = [RKManagedObjectStore defaultStore];
    RKEntityMapping *entityMapping = [RKEntityMapping mappingForEntityForName:@"Items" inManagedObjectStore:managedObjectStore];
    [entityMapping addAttributeMappingsFromDictionary:@@"Id":@"iD",
                                                        @"PicUrl":@"picURL",
                                                        @"CorrectAnswer":@"correctAnswer",
                                                        @"Difficulty":@"difficulty",
                                                        @"CategoryId":@"categoryID",
                                                        @"CategoryName":@"categoryName",
                                                        @"AccountId":@"accountID"
                                                        ];
    RKLogConfigureByName("RestKit/Network", RKLogLevelTrace);

    RKResponseDescriptor *responseDescriptor =[RKResponseDescriptor responseDescriptorWithMapping:entityMapping method:RKRequestMethodGET pathPattern:nil keyPath:nil statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];

    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"https:someDomain"]];
    RKManagedObjectRequestOperation *managedObjectRequestOperation =[[RKManagedObjectRequestOperation alloc]initWithRequest:request responseDescriptors:@[responseDescriptor]];
    managedObjectRequestOperation.managedObjectContext = self.managedObjectContext;
    [[NSOperationQueue currentQueue]addOperation:managedObjectRequestOperation];


【问题讨论】:

【参考方案1】:

这行日志告诉你 RestKit 不喜欢你的设置:

2013-10-16 05:07:32.783 Quizi[27422:1003] W restkit.object_mapping:RKMapperOperation.m:99 Adding mapping error: No mappable values found for any of the attributes or relationship mappings

问题是您需要将响应描述符的 keypath 设置为 @"Items",而您实际上已将其设置为 nil(因此 RestKit 无法找到要映射的正确数据)。

【讨论】:

再次感谢韦恩!像往常一样,问题是我的关键路径。我想我必须设置为零,因为 JSON 是嵌套的。

以上是关于tableviewcontroller 看不到 configureCell:atIndexPath (使用 restkit 的核心数据)的主要内容,如果未能解决你的问题,请参考以下文章

如何在Objective C中通过UIView获取UIAlertController?

从 tableviewcontroller 加载 tableviewcontroller 不起作用

我无法将 TableViewCell 按钮连接到 TableViewController!如何将 TableViewCell 中的值连接到 TableViewController?

将 TableViewController 添加到现有项目

RxSwift 以及如何制作简单的 TableViewController?

XCode 7 - TableViewController 错误