我有一个自定义的UITableViewController类,在我的应用程序的几个ViewControllers之间共享。如何将getData与私有标志变量分开?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
...
// Check if we are almost at the end of the scroll. If so, start fetching.
// Doing this here is better than overriding scrollViewDidEndDragging
if (indexPath.row >= [self.tableView numberOfRowsInSection:0] - 3) {
[self refill];
}
return cell;
}
我愿做先发制人取的用户已经达到了页面底部之前。
但是,我注意到如果用户再次向上滚动然后再滚下来,我的VC会触发另一次抓取。
所以我想出了一个标志isFetching,它检查以防止重复读取。不幸的是,我正在运行异步代码,并且我需要一种方法在完成取回操作后重置此标志。
FeedVC:
- (void)refill {
if (self.continueFetching && !self.isFetching) {
NSLog(@"Fetching started");
self.isFetching = true;
AFJSONRequestOperation *operation = [self.delegate refill:^{
NSLog(@"Fetching completed");
self.isFetching = false;
}];
[self.pendingOperations addObject:operation];
}
}
代表:
- (AFJSONRequestOperation *)refill:(void (^)(void))finishCallback {
return [[ItemStore sharedInstance] fetchItemsForFeed:^(int itemsFetched, int nextOffset, bool hasNextPage) {
finishCallback();
} withFailureBlock:^{
finishCallback();
}];
}
我觉得很痛苦围绕完成集团通过以翻转一个布尔值。有没有人有更好的建议?
我想你应该尝试落实UITableView的滚动视图的委托方法的渐进式笔芯像scrollviewdragended。 – iphonic 2013-04-06 04:03:19
这个逻辑可以设置在NSOperation的'completionBlock'上:'operation = [[ItemStore sharedInstance] fetchItemsForFeed:NULL withFailureBlock:NULL];''然后'[operation setCompletionBlock:yourBlock];' – 2013-04-06 04:55:08
此外,您可以保留对获取操作的引用,并在完成块中将其取消。这样你可以通过粒度来取消操作,如果需要的话。 – 2013-04-06 04:57:15