目前我正在学习本教程 http://www.appcoda.com/ios-programming-app-backend-parse/
但是当来自 parse.com 的数据自动更新时,我希望用新数据刷新 tableviewcontroller。我不希望用户总是不得不拉动刷新。
或者也许每分钟刷新一次?
听说reloaddata,但是代码怎么实现呢?
- (id)initWithCoderNSCoder *)aCoder
{
self = [super initWithCoder:aCoder];
if (self) {
// The className to query on
self.parseClassName = @"Recipe";
// 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;
}
- (PFQuery *)queryForTable
{
PFQuery *query = [PFQuery queryWithClassName:self.parseClassName];
return query;
}
- (UITableViewCell *)tableViewUITableView *)tableView cellForRowAtIndexPathNSIndexPath *)indexPath objectPFObject *)object
{
static NSString *simpleTableIdentifier = @"RecipeCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
// Configure the cell
PFFile *thumbnail = [object objectForKey"imageFile"];
PFImageView *thumbnailImageView = (PFImageView*)[cell viewWithTag:100];
thumbnailImageView.image = [UIImage imageNamed"placeholder.jpg"];
thumbnailImageView.file = thumbnail;
[thumbnailImageView loadInBackground];
UILabel *nameLabel = (UILabel*) [cell viewWithTag:101];
nameLabel.text = [object objectForKey"name"];
UILabel *prepTimeLabel = (UILabel*) [cell viewWithTag:102];
prepTimeLabel.text = [object objectForKey"prepTime"];
return cell;
}
在.h文件中
@property (nonatomic, strong) NSTimer *fetchTimer;
@property (nonatomic, strong) NSArray *items;
在 .m 文件中
- (void)viewDidLoad
{
NSTimer *timer = [NSTimer timerWithTimeInterval:3600
targetid)self
selectorselector(fetch)
userInfo:nil
repeats:YES];
self.fetchTimer = timer;
}
- (void)fetch
{
PFQuery *query = [PFObject query];
[query whereKey"" equalTo""];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
self.items = [NSArray arrayWithArraybjects];
[self.tableView reloadData];
}];
}
1) 设置一个 3600 间隔时间的 NSTimer 并触发获取数据功能。 2) 获取数据后只需调用 [self.tableView reloadData] 即可。这完全取决于您如何设计编码结构。
关于ios - PFQueryTableView 在新数据更新或每分钟使用 Parse 刷新时自动刷新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21751714/
欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) | Powered by Discuz! X3.4 |