IOS核心数据,UITableView加载更多数据

IOS核心数据,UITableView加载更多数据,ios,uitableview,core-data,Ios,Uitableview,Core Data,在我的IOS项目中,我使用核心数据。我有1000个元素 首先,我需要在UITableView中显示30个元素。当用户滚动并到达UITableView的底部(最后5个元素)时,我可以将新数据加载到表中。 我该怎么做 我使用了这个代码,但它并没有按照我想要的方式工作 #import "HomeViewController.h" #import "GDMnLineRKObjectManager.h" #import "CoreData+MagicalRecord.h" #import "CinName

在我的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
#导入“HomeViewController.h”
#导入“GDMnLineRKObjectManager.h”
#导入“CoreData+MagicalRecord.h”
#进口肉桂
#导入“Event.h”
@接口HomeViewController()
@属性(弱、非原子)IBUITableView*tableView;
@整数dpc的性质;
@属性BOOL是_load;
@属性布尔过程加载;
@属性NSFetchRequest*fetchRequest;
@属性(非原子)BOOL加载MoreTableViewData;
@属性(非原子)整数inf_计数器;
@结束
@HomeViewController的实现
//@综合桌面视图;
-(iAction)显示菜单
{
//关闭键盘(可选)
//
[自视图编辑:是];
[self.frostedViewController.view endEditing:是];
//显示视图控制器
//
[self.frostedViewController presentMenuViewController];
[self.tableView重载数据];
}
-(无效)未收到记忆警告{
[超级记忆警告];
}
-(作废)保存到存储
{
//保存到持久存储以供进一步使用。
}
-(无效)viewDidLoad{
[超级视图下载];
self.dpc=0;
self.inf_计数器=self.dpc;
self.is_负载=否;
self.process_加载=否;
self.fetchRequest=[NSFetchRequestFetchRequestWithEntityName:NSStringFromClass([Event class]);
//加载视图后,通常从nib执行任何其他设置。
/*
*/
[自加载元件];
////
[self.tableView setDelegate:self];
[self.tableView setDataSource:self];
///
}
-(空)荷载要素
{
//获取远程“字符”对象数组。指定偏移量。
[[GDMnLineRKObjectManager]getMnLineObjectsAtPath:服务器\u路径\u加载
参数:@{@“someval”:@(“564”)}
成功:^(RKObjectRequestOperation*操作,RKMappingResult*映射结果){
}
失败:^(RKObjectRequestOperation*操作,NSError*错误){
//未能加载字符。
/*
[自动画活动指示器:否];
[底部PullView完成加载];
*/
[[[UIAlertView alloc]initWithTitle:@“Marvel API错误”消息:operation.Error.localizedDescription委托:自取消按钮:@“取消”其他按钮:@“重试”,无]显示];
}];
}
#pragma标记-表视图
-(NSInteger)表格视图中的节数:(UITableView*)表格视图{
返回[[self.fetchedResultsController节]计数];
}
-(NSInteger)表视图:(UITableView*)表视图行数节:(NSInteger)节{
id sectionInfo=[self.fetchedResultsController sections][section];
返回[sectionInfo numberOfObjects];
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
NSLog(@“III=%@),indexath);
UITableViewCell*单元格=[tableView dequeueReusableCellWithIdentifier:@“HuCell”forIndexPath:indexPath];
[self-configureCell:cell-atIndexPath:indexPath];
if(indexath.row>self.inf\u计数器-5){
//用户已滚动到可用数据列表的底部,因此如果我们还没有加载,请模拟加载更多数据
如果(!self.loadingMoreTableViewData&&self.process\u loading==否){
self.loadingMoreTableViewData=YES;
self.process_load=是;
[self-performSelector:@selector(addSomeMoreEntriesToTableView),对象:nil afterDelay:0.0f];
}
}
if(indexPath.row- (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;
NSFetchRequest *fetchRequest = [self createFetchRequest];
NSFetchedResultsController *controller = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
                                                                                 managedObjectContext:context
                                                                                   sectionNameKeyPath:nil cacheName:nil];
[controller performFetch:nil];
self.controller = controller;