情况如下:
我有一个“UITableViewController”,它使用RestKits“RKFetchedResultsTableController”加载对象。单击一个单元格后,我切换到一个细节UITableViewController也由一个“RKFetchedResultsTableController”驱动,它为我提供了所选对象的相应答案文本。
现在的问题是,如果我回到第一个“UITableViewController”并在表格中选择另一个对象,那么前一个选定对象的旧答案文本就在详细信息表中。如果我使用“pullToRefresh”函数,表格将刷新并加载正确的答案。
为什么前一个对象的旧答案仍然在tableView中,而不是新选择对象的正确答案,即使我在viewWillAppear方法中告诉[tableController loadTable]。
答案 0 :(得分:0)
的AppDelegate:
@
interface AppDelegate ()
@property (nonatomic, strong, readwrite) RKObjectManager *objectManager;
@property (nonatomic, strong, readwrite) RKManagedObjectStore *objectStore;
@end;
@implementation AppDelegate
@synthesize window = _window, isAuthenticated;
@synthesize objectManager;
@synthesize objectStore;
- (void)initializeRestKit
{
//self.objectManager = [RKObjectManager managerWithBaseURLString:@"http://falling-ocean-1302.herokuapp.com"];
self.objectManager = [RKObjectManager managerWithBaseURLString:@"http://falling-ocean-1302.herokuapp.com"];
self.objectManager.serializationMIMEType = RKMIMETypeJSON;
self.objectManager.acceptMIMEType = RKMIMETypeJSON;
self.objectStore = [RKManagedObjectStore objectStoreWithStoreFilename:@"MMmtvzme.sqlite"];
self.objectManager.objectStore = self.objectStore;
self.objectManager.mappingProvider = [MMMappingProvider mappingProviderWithObjectStore:self.objectStore];
self.objectManager.client.cachePolicy = RKRequestCachePolicyNone;
RKLogConfigureByName("RestKit", RKLogLevelTrace);
RKLogConfigureByName("RestKit/Network", RKLogLevelTrace);
RKLogConfigureByName("RestKit/ObjectMapping", RKLogLevelTrace);
RKLogConfigureByName("RestKit/Network/Queue", RKLogLevelTrace);
// Enable automatic network activity indicator management
objectManager.client.requestQueue.showsNetworkActivityIndicatorWhenBusy = YES;
[objectManager.router routeClass:[MMRequest class] toResourcePath:@"/requests" forMethod:RKRequestMethodPOST];
[objectManager.router routeClass:[MMRequest class] toResourcePath:@"/requests" forMethod:RKRequestMethodDELETE];
[objectManager.router routeClass:[MMAnswer class] toResourcePath:@"/requests/:request_id/answers" forMethod:RKRequestMethodPOST];
}
好的,这是第一个UITableViewController的代码
@interface MMMyRequestList ()
@property (nonatomic, strong) RKFetchedResultsTableController *tableController;
@end
@implementation MMMyRequestList
@synthesize tableController;
- (void)viewDidLoad
{
[super viewDidLoad];
[self configureRKTableController];
[self configureCellMapping];
[self useCustomNib];
}
- (void)configureRKTableController{
self.tableController = [[RKObjectManager sharedManager] fetchedResultsTableControllerForTableViewController:self];
self.tableController.autoRefreshFromNetwork = YES;
self.tableController.pullToRefreshEnabled = YES;
self.tableController.resourcePath = @"/requests";
self.tableController.variableHeightRows = YES;
NSSortDescriptor *descriptor = [NSSortDescriptor sortDescriptorWithKey:@"request_id" ascending:NO];
self.tableController.sortDescriptors = [NSArray arrayWithObject:descriptor];
tableController.canEditRows = YES;
}
- (void)configureCellMapping{
RKTableViewCellMapping *cellMapping = [RKTableViewCellMapping cellMapping];
cellMapping.cellClassName = @"MMRequestCell";
cellMapping.reuseIdentifier = @"MMRequest";
cellMapping.rowHeight = 100.0;
[cellMapping mapKeyPath:@"title" toAttribute:@"requestLabel.text"];
[cellMapping mapKeyPath:@"user.first_name" toAttribute:@"userLabel.text"];
cellMapping.onSelectCellForObjectAtIndexPath = ^(UITableViewCell *cell, id object, NSIndexPath* indexPath)
{
MMMyRequestListAnswer * uic = [self.storyboard instantiateViewControllerWithIdentifier:@"MMMyRequestListAnswer"];
MMRequest *request = [self.tableController objectForRowAtIndexPath:indexPath];
if ([uic respondsToSelector:@selector(setRequest:)]) {
[uic setRequest:request];
}
[self.navigationController pushViewController:uic animated:YES];
};
[tableController mapObjectsWithClass:[MMRequest class] toTableCellsWithMapping:cellMapping];
}
- (void)useCustomNib{
[self.tableView registerNib:[UINib nibWithNibName:@"MMRequestCell" bundle:nil] forCellReuseIdentifier:@"MMRequest"];
}
- (void)objectLoader:(RKObjectLoader*)objectLoader didFailWithError:(NSError*)error {
UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"Error"
message:[error localizedDescription]
delegate:nil
cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
NSLog(@"Hit error: %@", error);
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
/**
Load the table view!
*/
[tableController loadTable];
}
单击一个单元格后,Detail UIViewController开始运作
interface MMMyRequestListAnswer ()
@property (nonatomic, strong) RKFetchedResultsTableController *tableController;
@end
@implementation MMMyRequestListAnswer
@synthesize tableHeaderView, requestTextLabel;
@synthesize request;
@synthesize tableController;
- (void)viewDidLoad
{
[super viewDidLoad];
[self configureRKTableController];
[self configureCellMapping];
[self useCustomNib];
}
- (void)configureRKTableController{
self.tableController = [[RKObjectManager sharedManager] fetchedResultsTableControllerForTableViewController:self];
self.tableController.autoRefreshFromNetwork = YES;
self.tableController.pullToRefreshEnabled = YES;
self.tableController.resourcePath = [NSString stringWithFormat:@"/requests/%i/answers", [self.request.request_id intValue]];
self.tableController.variableHeightRows = YES;
NSSortDescriptor *descriptor = [NSSortDescriptor sortDescriptorWithKey:@"answer_id" ascending:NO];
self.tableController.sortDescriptors = [NSArray arrayWithObject:descriptor];
}
- (void)configureCellMapping{
RKTableViewCellMapping *cellMapping = [RKTableViewCellMapping cellMapping];
cellMapping.cellClassName = @"MMRequestAnswerCell";
cellMapping.reuseIdentifier = @"MMAnswer";
cellMapping.rowHeight = 80.0;
[cellMapping mapKeyPath:@"text" toAttribute:@"answerLabel.text"];
[cellMapping mapKeyPath:@"user.first_name" toAttribute:@"userLabel.text"];
[tableController mapObjectsWithClass:[MMAnswer class] toTableCellsWithMapping:cellMapping];
}
- (void)useCustomNib{
[self.tableView registerNib:[UINib nibWithNibName:@"MMRequestAnswerCell" bundle:nil] forCellReuseIdentifier:@"MMAnswer"];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
/**
Load the table view!
*/
[tableController loadTable];
}
对象映射在此类中处理:
@implementation MMMappingProvider
@synthesize objectStore;
+ (id)mappingProviderWithObjectStore:(RKManagedObjectStore *)objectStore {
return [[self alloc] initWithObjectStore:objectStore];
}
- (id)initWithObjectStore:(RKManagedObjectStore *)theObjectStore {
self = [super init];
if (self) {
self.objectStore = theObjectStore;
[self setObjectMapping:[self requestObjectMapping] forResourcePathPattern:@"/requests" withFetchRequestBlock:^NSFetchRequest *(NSString *resourcePath) {
NSFetchRequest *fetchRequest = [MMRequest fetchRequest];
return fetchRequest;
}];
[self setObjectMapping:[self answerObjectMapping] forResourcePathPattern:@"/requests/:request_id/answers" withFetchRequestBlock:^NSFetchRequest *(NSString *resourcePath) {
NSFetchRequest *fetchRequest = [MMAnswer fetchRequest];
fetchRequest.sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"answer_id" ascending:YES]];
return fetchRequest;
}];
[self setSerializationMapping:[self.requestObjectMapping inverseMapping] forClass:[MMRequest class]];
[self setSerializationMapping:[self.answerObjectMapping inverseMapping] forClass:[MMAnswer class]];
[self setSerializationMapping:[self.userObjectMapping inverseMapping] forClass:[MMUser class]];
}
return self;
}
- (RKManagedObjectMapping *)userObjectMapping {
RKManagedObjectMapping *mapping = [RKManagedObjectMapping mappingForEntityWithName:@"MMUser" inManagedObjectStore:self.objectStore];
mapping.primaryKeyAttribute = @"user_id";
[mapping mapAttributes:@"first_name", nil];
[mapping mapKeyPathsToAttributes:
@"id", @"user_id",
nil];
return mapping;
}
- (RKManagedObjectMapping *)answerObjectMapping {
RKManagedObjectMapping *mapping = [RKManagedObjectMapping mappingForEntityWithName:@"MMAnswer" inManagedObjectStore:self.objectStore];
mapping.primaryKeyAttribute = @"answer_id";
[mapping mapAttributes:@"text",@"request_id",@"user_id", nil];
[mapping mapKeyPathsToAttributes:
@"id", @"answer_id",
nil];
[mapping mapKeyPath:@"user" toRelationship:@"user" withMapping:[self userObjectMapping]];
[mapping mapKeyPath:@"request" toRelationship:@"request" withMapping:[self requestObjectMapping]];
return mapping;
}
- (RKManagedObjectMapping *)requestObjectMapping {
RKManagedObjectMapping *mapping = [RKManagedObjectMapping mappingForEntityWithName:@"MMRequest" inManagedObjectStore:self.objectStore];
mapping.primaryKeyAttribute = @"request_id";
[mapping mapAttributes:@"title",@"user_id", nil];
[mapping mapKeyPathsToAttributes:
@"id", @"request_id",
nil];
[mapping mapKeyPath:@"user" toRelationship:@"user" withMapping:[self userObjectMapping]];
return mapping;
}
答案 1 :(得分:0)
好想出来!!!一些挖掘显示我在映射提供程序之前加载了我的UITableviewcontroller。
修复是,在[BOOL]应用程序中使用[self initialiserestkit]方法:( UIApplication *)应用程序didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
并将其放在此方法的任何代码之前(即make [self initialiserestkit]是didfinishlaunchingwithoptions方法的第一行。
问题解决了。现在tableview在映射之后加载,所以一切正常。