在我使用Core Data的IOS项目中。我有1000个元素 首先,我需要在UITableView 30元素中显示。当用户滚动并到达UITableView的底部(5个元素到结尾)时,我可以将新数据加载到表中。 我怎么能这样做
我使用了这段代码但它没有按我想要的方式工作
#import "HomeViewController.h"
#import "GDMnLineRKObjectManager.h"
#import "CoreData+MagicalRecord.h"
#import "CinNames.h"
#import "Event.h"
@interface HomeViewController ()<UITableViewDataSource, UITableViewDelegate, NSFetchedResultsControllerDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property NSUInteger dpc;
@property BOOL is_load;
@property BOOL process_loading;
@property NSFetchRequest *fetchRequest;
@property (nonatomic) BOOL loadingMoreTableViewData;
@property (nonatomic) NSUInteger inf_counter;
@end
@implementation HomeViewController
//@synthesize tableView;
- (IBAction)showMenu
{
// Dismiss keyboard (optional)
//
[self.view endEditing:YES];
[self.frostedViewController.view endEditing:YES];
// Present the view controller
//
[self.frostedViewController presentMenuViewController];
[self.tableView reloadData];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (void)saveToStore
{
// Saving to persistent store for further usage.
}
- (void)viewDidLoad {
[super viewDidLoad];
self.dpc = 0;
self.inf_counter = self.dpc;
self.is_load = NO;
self.process_loading = NO;
self.fetchRequest = [NSFetchRequest fetchRequestWithEntityName:NSStringFromClass([Event class])];
// Do any additional setup after loading the view, typically from a nib.
/*
*/
[self loadElements];
////
[self.tableView setDelegate:self];
[self.tableView setDataSource:self];
///
}
- (void)loadElements
{
// Get an array of remote "character" objects. Specify the offset.
[[GDMnLineRKObjectManager manager] getMnLineObjectsAtPath:SERVER_PATH_LOAD
parameters:@{@"someval" : @("564")}
success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
}
failure:^(RKObjectRequestOperation *operation, NSError *error) {
// Failed to load characters.
/*
[self animateActivityIndicator:NO];
[bottomPullView finishedLoading];
*/
[[[UIAlertView alloc] initWithTitle:@"Marvel API Error" message:operation.error.localizedDescription delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Retry", nil] show];
}];
}
#pragma mark - Table View
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [[self.fetchedResultsController sections] count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
id <NSFetchedResultsSectionInfo> sectionInfo = [self.fetchedResultsController sections][section];
return [sectionInfo numberOfObjects];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"III = %@", indexPath);
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HuCell" forIndexPath:indexPath];
[self configureCell:cell atIndexPath:indexPath];
if (indexPath.row > self.inf_counter - 5) {
// User has scrolled to the bottom of the list of available data so simulate loading some more if we aren't already
if (!self.loadingMoreTableViewData && self.process_loading == NO) {
self.loadingMoreTableViewData = YES;
self.process_loading = YES;
[self performSelector:@selector(addSomeMoreEntriesToTableView) withObject:nil afterDelay:0.0f];
}
}
if (indexPath.row < self.inf_counter) {
[self configureCell:cell atIndexPath:indexPath];
} else {
cell.textLabel.text = @"Loading more data...";
}
return cell;
}
- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {
NSManagedObject *object = [self.fetchedResultsController objectAtIndexPath:indexPath];
NSString * string3 = [NSString stringWithFormat:@"%@ - %ld", [[object valueForKey:@"name"] description], (long)indexPath.row];
cell.textLabel.text = string3;//[[object valueForKey:@"name"] description];
}
#pragma mark - Fetched results controller
- (NSFetchedResultsController *)fetchedResultsController
{
NSLog(@"Skolko");
if ((!_fetchedResultsController || self.process_loading == YES)) {
self.process_loading = NO;
self.is_load = NO;
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"timeStamp" ascending:NO];
self.fetchRequest.sortDescriptors = @[sortDescriptor];
self.fetchRequest.fetchLimit = self.dpc;
//self.fetchRequest.fetchBatchSize = 30;
self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:self.fetchRequest managedObjectContext:[RKManagedObjectStore defaultStore].mainQueueManagedObjectContext sectionNameKeyPath:nil cacheName:nil];
self.fetchedResultsController.delegate = self;
NSError *error;
[self.fetchedResultsController performFetch:&error];
NSLog(@"%@",[self.fetchedResultsController fetchedObjects]);
NSLog(@"Counta: %lu",(unsigned long)[self.fetchedResultsController.fetchedObjects count]);
NSAssert(!error, @"Error performing fetch request: %@", error);
self.inf_counter = [self.fetchedResultsController.fetchedObjects count];
[self.tableView reloadData];
}
return _fetchedResultsController;
}
- (void)addSomeMoreEntriesToTableView {
self.dpc += 20;
[self fetchedResultsController];
self.loadingMoreTableViewData = NO;
[self.tableView reloadData];
}
- (void)scrollViewDidScroll: (UIScrollView*)scroll {
// UITableView only moves in one direction, y axis
CGFloat currentOffset = scroll.contentOffset.y;
CGFloat maximumOffset = scroll.contentSize.height - scroll.frame.size.height;
NSLog(@"hui = %f", (maximumOffset - currentOffset));
// Change 10.0 to adjust the distance from bottom
if (maximumOffset - currentOffset <= 10.0) {
self.is_load = YES;
self.dpc += 10;
[self fetchedResultsController];
//[self.tableView reloadData];
}
}
@end
答案 0 :(得分:1)
您要找的是NSFetchRequest
上的fetchBatchSize属性。我希望你也使用NSFetchedResultsController
,它与表视图和集合视图非常相似。这是描述:
您使用获取的结果控制器来有效地管理结果 从Core Data获取请求返回以提供数据 UITableView对象。
因此,在设置获取结果控制器之前,只需确保设置fetch请求的fetchBatchSize并让框架为您处理所有优化。
修改强>
OP希望原始提取仅包含30个项目,然后仅当用户滚动它时才应重新填充提取。在大多数情况下,fetchBatchSize应该是解决方案,因为核心数据将寻求对所有结果进行故障,从而避免获取所有对象的开销,而不是倾向于错误地将它们作为错误。为了坚持OP,解决方案可能是这样的:
- (NSFetchRequest *)createFetchRequest {
NSFetchRequest *fetch = [NSFetchRequest fetchRequestWithEntityName:NSStringFromClass([MyEntity class])];
fetch.predicate = //set up your fetch
return fetch;
}
然后,当您实例化控制器时,您可以设置获取限制:
NSFetchRequest *fetchRequest = [self createFetchRequest];
fetchRequest.fetchLimit = 30;
NSFetchedResultsController *controller = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
managedObjectContext:context
sectionNameKeyPath:nil cacheName:nil];
self.controller = controller;
因为控制器上的fetchRequest
属性是只读的,所以当你想要获取所有东西时,你被迫重新分配你的控制器:
NSFetchRequest *fetchRequest = [self createFetchRequest];
NSFetchedResultsController *controller = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
managedObjectContext:context
sectionNameKeyPath:nil cacheName:nil];
[controller performFetch:nil];
self.controller = controller;
如果你想继续学习更多内容,请前往Ray Wenderlich,他们为iOS提供了很棒的教程。
快乐编码
答案 1 :(得分:0)
选项:
观看来自WWDC 2011的高级ScrollView技术会议。
教程:http://mobiledevelopertips.com/user-interface/creating-circular-and-infinite-uiscrollviews.html