gpt4 book ai didi

ios - 使用缓存实现 PFQueryTableViewController

转载 作者:行者123 更新时间:2023-11-28 22:05:36 25 4
gpt4 key购买 nike

用 PFQueryTableViewController 替换我的 TableViewController 时遇到了一些麻烦,但我通过拉动刷新和分页得到了它。但是,我无法让缓存工作(在 .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

最佳答案

从外观上看,您没有使用 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];
}];
}

关于ios - 使用缓存实现 PFQueryTableViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24112686/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com