使用PFQueryTableViewController替换我的TableViewController有点麻烦,但是我使用了pull to refresh和pagination。但是,我无法使缓存工作(在.m文件的最底部)。关于什么事情的任何想法?
我在这里做的唯一其他改变是将单元格中的图像子类化为PFImage。
ContactsTableViewController.h
#import <UIKit/UIKit.h>
#import <Parse/Parse.h>
#import "FilterViewController.h"
@interface ContactsTableViewController : PFQueryTableViewController <UITableViewDelegate>
@property (nonatomic) NSString *titleName;
@property (nonatomic) NSString *date;
@property (nonatomic) NSString *price;
@property (nonatomic) NSString *imageName;
@property (nonatomic) NSString *venue;
@property (nonatomic) UIImage *image;
@property (nonatomic) NSString *filter;
@property (nonatomic) NSArray *eventsArray;
-(void)retrieveEvents;
-(void)filterEvents;
@end
ContactsTableViewController.m
#import "ContactsTableViewController.h"
#import "CustomCellTableViewCell.h"
#import "DetailViewController.h"
@interface ContactsTableViewController ()
@end
@implementation ContactsTableViewController
- (id)initWithCoder:(NSCoder *)aCoder
{
self = [super initWithCoder:aCoder];
if (self) {
// The className to query on
self.parseClassName = @"eventsList";
// The key of the PFObject to display in the label of the default cell style
// self.textKey = @"name";
// Whether the built-in pull-to-refresh is enabled
self.pullToRefreshEnabled = YES;
// Whether the built-in pagination is enabled
self.paginationEnabled = NO;
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self performSelector:@selector(retrieveEvents)];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(filterEvents) name:@"updateParent" object:nil];
self.view.backgroundColor = [UIColor colorWithRed:245/255.0 green:243/255.0 blue:240/255.0 alpha:1];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [self.eventsArray count];
}
// Parse Method
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object
{
static NSString *CellIdentifier = @"cellID";
CustomCellTableViewCell *customCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
NSDictionary *tempDict = [self.eventsArray objectAtIndex:indexPath.row];
self.titleName = [tempDict objectForKey:@"eventTitle"];
self.price = [tempDict objectForKey:@"eventPrice"];
self.date = [tempDict objectForKey:@"eventDate"];
self.venue = [tempDict objectForKey:@"eventVenue"];
PFFile *imageFile = [tempDict objectForKey:@"eventImage"];
[imageFile getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
if (!error) {
customCell.customImageView.image = [UIImage imageWithData:data];
}
}];
customCell.titleLabel.text = self.titleName;
customCell.priceLabel.text = self.price;
customCell.customDateLabel.text = self.date;
customCell.venueNameLabel.text = self.venue;
return customCell;
}
- (void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
[cell setBackgroundColor:[UIColor darkGrayColor]];
tableView.separatorColor = [UIColor clearColor];
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"showEventDetail"]) {
DetailViewController *destinationVC = [[DetailViewController alloc] init];
destinationVC = segue.destinationViewController;
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
NSDictionary *tempDict = [self.eventsArray objectAtIndex:indexPath.row];
destinationVC.eventTitle = [tempDict objectForKey:@"eventTitle"];
destinationVC.eventPrice = [tempDict objectForKey:@"eventPrice"];
destinationVC.eventDate = [tempDict objectForKey:@"eventDate"];
destinationVC.venueName = [tempDict objectForKey:@"eventVenue"];
destinationVC.navigationItem.title = [tempDict objectForKey:@"eventTitle"];
// Image
PFFile *imageFile = [tempDict objectForKey:@"eventImage"];
[imageFile getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
if (!error) {
destinationVC.eventImageView.image = [UIImage imageWithData:data];
destinationVC.eventImage = [UIImage imageWithData:data];
}
}];
// GeoPoint
destinationVC.geoPoint = [tempDict objectForKey:@"GeoPoint"];
} else if ([segue.identifier isEqualToString:@"showFilterTable"]){
FilterViewController *vc = (FilterViewController *)[[[segue destinationViewController] viewControllers] objectAtIndex:0];
[vc setDelegate:self];
}
}
-(void)retrieveEvents
{
PFQuery *retrieveEvents = [PFQuery queryWithClassName:@"eventsList"];
[retrieveEvents setLimit:300];
[retrieveEvents orderByAscending:@"eventTitle"];
[retrieveEvents findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
self.eventsArray = [[NSArray alloc] initWithArray:objects];
}
[self.tableView reloadData];
}];
}
-(void)filterEvents
{
if ([self.filter isEqualToString:@"All Events"]) {
[self performSelector:@selector(retrieveEvents)];
return;
}
NSLog(@"retrieveEvents: %@", self.filter);
PFQuery *retrieveEvents = [PFQuery queryWithClassName:@"eventsList"];
[retrieveEvents whereKey:@"eventType" equalTo:self.filter];
[retrieveEvents setLimit:300];
[retrieveEvents orderByAscending:@"eventTitle"];
[retrieveEvents findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
self.eventsArray = [[NSArray alloc] initWithArray:objects];
NSLog(@"%@", self.eventsArray);
}
[self.tableView reloadData];
}];
}
// Parse Method
- (PFQuery *)queryForTable
{
PFQuery *query = [PFQuery queryWithClassName:self.parseClassName];
query.cachePolicy = kPFCachePolicyCacheThenNetwork;
return query;
}
@end
答案 0 :(得分:1)
kPFCachePolicyCacheThenNetwork
意味着同时使用缓存和网络(首先从缓存中获取速度,然后在下次从网络获取最新信息)。请尝试使用kPFCachePolicyCacheElseNetwork
。
答案 1 :(得分:1)
从它的外观来看,你没有使用queryForTable来填充tableview,你正在使用单独的查询。
此查询永远不会在您的tableview中使用
- (PFQuery *)queryForTable
{
PFQuery *query = [PFQuery queryWithClassName:self.parseClassName];
query.cachePolicy = kPFCachePolicyCacheThenNetwork;
return query;
}
相反,这个人做了:
-(void)retrieveEvents
{
PFQuery *retrieveEvents = [PFQuery queryWithClassName:@"eventsList"];
[retrieveEvents setLimit:300];
[retrieveEvents orderByAscending:@"eventTitle"];
[retrieveEvents findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
self.eventsArray = [[NSArray alloc] initWithArray:objects];
}
[self.tableView reloadData];
}];
}
答案 2 :(得分:0)
试试这段代码。它可能不完美。但我相信至少应该让你朝着正确的方向前进。
#import "ContactsTableViewController.h"
#import "CustomCellTableViewCell.h"
#import "DetailViewController.h"
@interface ContactsTableViewController ()
@end
@implementation ContactsTableViewController
- (id)initWithCoder:(NSCoder *)aCoder
{
self = [super initWithCoder:aCoder];
if (self) {
// The className to query on
self.parseClassName = @"eventsList";
// The key of the PFObject to display in the label of the default cell style
// self.textKey = @"name";
// Whether the built-in pull-to-refresh is enabled
self.pullToRefreshEnabled = YES;
// Whether the built-in pagination is enabled
self.paginationEnabled = NO;
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(filterEvents) name:@"updateParent" object:nil];
self.view.backgroundColor = [UIColor colorWithRed:245/255.0 green:243/255.0 blue:240/255.0 alpha:1];
tableView.separatorColor = [UIColor clearColor];
}
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
//This reloaded the PFQueryTableView
[self loadObjects];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return self.objects.count;
}
// Parse Method
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object
{
static NSString *CellIdentifier = @"cellID";
CustomCellTableViewCell *customCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
PFObject *object = [self objectAtIndexPath:indexPath];
customCell.titleLabel.text = object[@"eventTitle"];
customCell.priceLabel.text = object[@"eventPrice"];
customCell.customDateLabel.text = object[@"eventDate"];
customCell.venueNameLabel.text = object[@"eventVenue"];
PFFile *imageFile = [tempDict objectForKey:@"eventImage"];
[imageFile getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
if (!error) {
customCell.customImageView.image = [UIImage imageWithData:data];
}
}];
return customCell;
}
- (void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
[cell setBackgroundColor:[UIColor darkGrayColor]];
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"showEventDetail"]) {
DetailViewController *destinationVC = [[DetailViewController alloc] init];
destinationVC = segue.destinationViewController;
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
NSDictionary *tempDict = [self.eventsArray objectAtIndex:indexPath.row];
destinationVC.eventTitle = [tempDict objectForKey:@"eventTitle"];
destinationVC.eventPrice = [tempDict objectForKey:@"eventPrice"];
destinationVC.eventDate = [tempDict objectForKey:@"eventDate"];
destinationVC.venueName = [tempDict objectForKey:@"eventVenue"];
destinationVC.navigationItem.title = [tempDict objectForKey:@"eventTitle"];
// Image
PFFile *imageFile = [tempDict objectForKey:@"eventImage"];
[imageFile getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
if (!error) {
destinationVC.eventImageView.image = [UIImage imageWithData:data];
destinationVC.eventImage = [UIImage imageWithData:data];
}
}];
// GeoPoint
destinationVC.geoPoint = [tempDict objectForKey:@"GeoPoint"];
} else if ([segue.identifier isEqualToString:@"showFilterTable"]){
FilterViewController *vc = (FilterViewController *)[[[segue destinationViewController] viewControllers] objectAtIndex:0];
[vc setDelegate:self];
}
}
// Parse Method
-(NSString *)filter
{
return _filter;
}
-(void)setFilter
{
_filter = filter;
[self loadObjects];
}
- (PFQuery *)queryForTable
{
PFQuery *query = [PFQuery queryWithClassName:self.parseClassName];
query.cachePolicy = kPFCachePolicyCacheThenNetwork;
if (self.filter && ![self.filter isEqualToString:@"All Events"]) [query whereKey:@"eventTitle" equalTo:self.filter];
[query setLimit:300];
[query orderByAscending:@"eventTitle"];
return query;
}
@end