2014-02-13 40 views
2

目前林下面這個教程http://www.appcoda.com/ios-programming-app-backend-parse/PFQueryTableView自動刷新當新數據更新或刷新每分鐘使用解析

但我想tableviewcontroller用新的數據更新時,從parse.com數據是自動更新。我不希望用戶總是不得不拉動刷新。

或者可能每分鐘都有時間刷新?

我聽說reloaddata,但如何實現代碼?

- (id)initWithCoder:(NSCoder *)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 *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)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; 
} 
+0

不確定我會每分鐘檢查一次,但您是否嘗試添加「NSTimer」? – Wain

+0

您也許可以安排雲代碼中的後臺作業,並且如果數據更新,可能會觸發靜默推送通知? – Moonwalkr

回答

1

在h文件

@property (nonatomic, strong) NSTimer *fetchTimer; 
@property (nonatomic, strong) NSArray *items; 

在.m文件

- (void)viewDidLoad 
{ 
    NSTimer *timer = [NSTimer timerWithTimeInterval:3600 
              target:(id)self 
              selector:@selector(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 arrayWithArray:objects]; 
     [self.tableView reloadData]; 
    }]; 
} 

1)設置的NSTimer與3600 intervalTime並觸發獲取數據的功能。 2)當數據被提取時,只需調用[self.tableView reloadData]。這完全取決於你如何設計你的編碼結構。