使用Parse.com

时间:2015-07-19 18:43:15

标签: objective-c uitableview parse-platform refresh pfquerytableviewcontrolle

我正在按照此示例获取分段表格视图:https://parse.com/questions/using-pfquerytableviewcontroller-for-uitableview-sections

我在添加第一个对象时重新加载表视图时遇到问题。

viewWillAppear我致电[self loadObjects],在objectsDidLoad结束时,我致电[self.tableView reloadData],但桌子仍然没有刷新。如果我杀了应用程序并重新启动它就可以完美运行。

我需要做什么才能在初次使用应用程序时刷新表格?

编辑:

这是我的代码:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self) { 
        // The className to query on
        self.parseClassName = @"TheClassName";

        // Whether the built-in pull-to-refresh is enabled
        self.pullToRefreshEnabled = YES;

        // Whether the built-in pagination is enabled
        self.paginationEnabled = NO;

        self.shouldReloadOnAppear = NO;

        self.sections = [NSMutableDictionary dictionary];
        self.sectionToFinanceTypeMap = [NSMutableDictionary dictionary];
    }

    return self;
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self loadObjects];    
    [self.tableView reloadData];
}

#pragma mark - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.sections.allKeys.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSString *financeType = [self financeTypeForSection:section];
    NSArray *rowIndecesInSection = [self.sections objectForKey:financeType];
    return rowIndecesInSection.count;
}

#pragma mark - PFQueryTableViewController

- (PFQuery *)queryForTable { 
    PFQuery *query = [PFQuery queryWithClassName:self.parseClassName];
    [query whereKey:@"user" equalTo:[PFUser currentUser]];
    [query orderByDescending:@"financeType"];

    // A pull-to-refresh should always trigger a network request.
    [query setCachePolicy:kPFCachePolicyNetworkOnly];

    // If no objects are loaded in memory, we look to the cache first to fill the table
    // and then subsequently do a query against the network.
    //
    // If there is no network connection, we will hit the cache first.
    if (self.sections.allKeys.count == 0 || ![[UIApplication sharedApplication].delegate performSelector:@selector(isParseReachable)]) {
        [query setCachePolicy:kPFCachePolicyCacheThenNetwork];
    }

    return query;
}

- (void)objectsDidLoad:(nullable NSError *)error {
    [super objectsDidLoad:error];

    // This method is called every time objects are loaded from Parse via the PFQuery

    [self.sections removeAllObjects];
    [self.sectionToFinanceTypeMap removeAllObjects];

    NSInteger section = 0;
    NSInteger rowIndex = 0;
    for (PFObject *object in self.objects) {
        NSString *financeType = [object objectForKey:@"financeType"];
        NSMutableArray *objectsInSection = [self.sections objectForKey:financeType];
        if (!objectsInSection) {
            objectsInSection = [NSMutableArray array];

            // this is the first time we see this financeType - increment the section index
            [self.sectionToFinanceTypeMap setObject:financeType forKey:[NSNumber numberWithInteger:section++]];

        }

        [objectsInSection addObject:[NSNumber numberWithInteger:rowIndex++]];

        [self.sections setObject:objectsInSection forKey:financeType];
    }

    [self.tableView reloadData];
}

- (PFObject *)objectAtIndexPath:(NSIndexPath *)indexPath {
    NSString *sportType = [self financeTypeForSection:indexPath.section];
    NSArray *rowIndecesInSection = [self.sections objectForKey:financeType];
    NSNumber *rowIndex = [rowIndecesInSection objectAtIndex:indexPath.row];
    return [self.objects objectAtIndex:[rowIndex intValue]];
}

0 个答案:

没有答案