【问题标题】:UITableView is not reloadingUITableView 没有重新加载
【发布时间】:2012-05-19 08:54:08
【问题描述】:

情况如下:

我有一个“UITableViewController”,它使用 RestKits“RKFetchedResultsTableController”加载对象。单击一个单元格后,我切换到一个详细的 UITableViewController,它也由“RKFetchedResultsTableController”驱动,它为我提供了所选对象的相应答案文本。

现在的问题是,如果我回到第一个“UITableViewController”并在表中选择另一个对象,则先前选择的对象中的旧答案文本位于详细信息表中。如果我使用“pullToRefresh”函数,表格会被刷新并且正确的答案正在加载。

即使我在 viewWillAppear 方法中告诉 [tableController loadTable],为什么上一个对象的旧答案仍在 tableView 中,而不是新选定对象的正确答案。

【问题讨论】:

    标签: refresh uitableview reload restkit


    【解决方案1】:

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

    【讨论】:

    • 我注意到的一件事是,当核心数据不包含数据时。重新加载工作正常。但是一旦对象存储在核心数据中,如果有第一篇文章中提到的问题。
    【解决方案2】:

    好的,想通了!!!一些挖掘显示我在映射提供程序之前加载了我的 UITableviewcontroller。

    修复方法是,在 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 中采用 [self initialiserestkit] 方法

    并将其放在此方法中的任何代码之前(即,将 [self initialiserestkit] 设为 didfinishlaunchingwithoptions 方法的第一行。

    问题已解决。现在表格视图在映射之后加载,所以一切正常。

    【讨论】:

    • 刚刚意识到这个答案可能不适用于您:(。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-26
    • 1970-01-01
    相关资源
    最近更新 更多