如何异步加载数据UITableview滚动和目标c中的核心数据获取

时间:2017-05-01 10:48:38

标签: ios objective-c iphone core-data

我的应用中有核心数据库。在该数据库中,一个模型CourseEvents包含超过250000条记录,每条记录包含大约5个字段。

每个UITableViewCell的记录值。

但我不想在一次获取请求中获取所有记录。想要根据UITableView滚动获取一些N个记录。

示例:

当表视图加载第一次想要获取20条记录时,每当用户滚动UITableView需要获取下一条20条记录时,就像需要根据UITableview的滚动从模型中获取数据。

我怎样才能做到这一点。请帮助.....

3 个答案:

答案 0 :(得分:1)

试试这个:

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == arrObject.count-1) {
        if (arrObject.count  < totalDataCount) {
            currentPage++; // initial value is zero
            [self getDataFromResource]; // call api here
        }
    }
}

答案 1 :(得分:0)

//Try this:-

//使用谓词,您可以根据滚动从核心数据中获取20条记录。

     -(void)viewDidLoad 
        {
           [super viewDidLoad];

         UIRefreshControl *refreshControl;
            [refreshControl addTarget:self action:@selector(refreshTable) forControlEvents:UIControlEventValueChanged];

            minId = 0;
            maxId = 20;
            maxId = minId +maxId;
            NSPredicate*preicate = [NSPredicate predicateWithFormat:@"id == %d && id < %d",minId,maxId];// id is your unique id in core data.
          //  Fetch first 20 records from core data using predicate.
        }

        - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
        {
            float bottomEdge = scrollView.contentOffset.y + scrollView.frame.size.height;
            if (bottomEdge >= self.tableView.contentSize.height) //this will get last record of tableview when scroll
                {
                    minId = maxId;
                    maxId = minId + maxId;
                    NSPredicate*preicate = [NSPredicate predicateWithFormat:@"id == %d && id < %d",minId,maxId];// id is your unique id in core data.
                    //  Fetch next 20 records from core data using predicate.
    [self.tableView reloadData];

                }
        }

   // Whenever scroll down ,refresh method is called

    - (void)refreshTable {
      [self.yourArray removeAllobjects];
    minId = 0;
            maxId = 20;
            maxId = minId +maxId;
            NSPredicate*preicate = [NSPredicate predicateWithFormat:@"id == %d && id < %d",minId,maxId];// id is your unique id in core data.
          //  Fetch first 20 records from core data using predicate.

    [self.tableView reloadData]

    }

答案 2 :(得分:0)

@satya murty   试试这个:-     uitableviewcell使用核心数据异步。

    - (UITableViewCell *)tableView:(UITableView *)tableView  cellForRowAtIndexPath:(NSIndexPath *)indexPath
        {
            static NSString *CellIdentifier = @"Cell";
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

            cell.tag = indexPath.row;
            //UserRecords TableName
            //self.yourArray records array (fetch from core data)
            UserRecords*records = self.yourArray[indexPath.row];
            if (records)
            {
                cell.imageView.image = nil;
                dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
                dispatch_async(queue, ^(void) {

                    NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:records.imageURL];

                                         UIImage* image = [[UIImage alloc] initWithData:imageData];
                                         if (image) {
                                             dispatch_async(dispatch_get_main_queue(), ^{
                                                 if (cell.tag == indexPath.row) {
                                                     cell.imageView.image = image;
                                                     [cell setNeedsLayout];
                                                 }
                                             });
                                         }
                });
                cell.textLabel.text = records.title;
            }
return cell;
        }