2012-05-10 3 views
2

をリロードされていない状況です。のUITableViewはここ

私はRestKits「RKFetchedResultsTableController」でオブジェクトをロード「のUITableViewController」を持っています。セルをクリックすると、選択されたオブジェクトの対応する応答テキストを表示する「RKFetchedResultsTableController」によって駆動される詳細UITableViewControllerに切り替わります。

最初の「UITableViewController」に戻って、テーブル内の別のオブジェクトを選択すると、前回選択したオブジェクトの古い回答テキストが詳細テーブルに表示される問題が解決しました。 "pullToRefresh"関数を使用すると、テーブルがリフレッシュされ、正しい答えが読み込まれます。

viewWillAppearメソッドで[tableController loadTable]を指定しても、以前のオブジェクトからの古い回答はまだtableViewにあり、新しい選択されたオブジェクトに対する正解ではないのはなぜですか。

+0

私達にあなたのコードを表示します。私が気づいた – lawicko

答えて

0

AppDelegate:

[OK]を

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]; 

    } 

からのコードは、アクションに入って来るの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;  
} 
+0

一つのことコアデータがデータを含まない場合、リロードはwoですうまくいく。しかし、オブジェクトがコアデータに格納されるとすぐに、最初のポストで説明した問題が発生します。 –

0

OK掘り出し物によっては、マッピングプロバイダの前にUITableviewControllerをロードしていることが明らかになりました。

修正は内法[自己initialiserestkit]を取ることだった - (BOOL)をアプリケーション:(のUIApplication *)アプリケーションdidFinishLaunchingWithOptions:(NSDictionaryの*)をlaunchOptions

と(この方法では、コードのいずれかの前にそれを置きますすなわち[自己initialiserestkit] didfinishlaunchingwithoptions方法の最初の行を作る。

解決の問題。すべてはそれが必要として動作して今すぐテーブルビューは、マッピング後にロードされる。

+0

この回答はあなたには当てはまりません。 – AdiTheExplorer

関連する問題