2016-05-21 5 views
0

Webサービスから取得するデータを格納および取得するためにコアデータを使用しています。 これらのデータをテーブルビューで表示します。 以下は私のcellForRowAtIndexPathメソッドです。膨大な数のデータを処理するためにUITableviewを最適化する

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    StatutoryMappingCell * cell = [tableView dequeueReusableCellWithIdentifier:@"myCell"]; 
    if (!cell) 
    { 
     [tableView registerNib:[UINib nibWithNibName:@"KnowledgeMainCell" bundle:nil] forCellReuseIdentifier:@"myCell"]; 
     cell = [tableView dequeueReusableCellWithIdentifier:@"myCell"]; 
    } 
    cell.statMapping.text = [self.items objectAtIndex:indexPath.row]; 

    return cell; 
} 

self.itemstableviewにロードする必要がある件のデータが含まれています。

イムフェーシングがあるという問題は、

  1. Imは2000のレコードのようなもので、サーバからのデータの巨大なセットを取得。したがって、tableviewの読み込みは非常に遅くなっています。どのように私はそのような巨大なレコードを処理するために上記のコードを最適化できますか?
  2. iOSでページ設定を行う方法はありますか?ユーザーがFacebookのように多くのコンテンツを取得するために下部にある[詳細]ボタンをクリックすることができますか?
+0

はここにあなたが遅延ローディングやプリフェッチローダー法のために行く必要があり、このリンク、http://stackoverflow.com/questions/33527262/uitableview-with-magicalrecord-huge-data-set?rq=1 –

+0

を参照してください –

+0

いくつか例を挙げてください。 – Roger

答えて

1

@MGR、NsfetchedresultsControllerを使用すると、それを達成できます。

ここでは、CDからデータを照会することができます。スウィフトのために

:Objective Cのために

lazy var fetchedResultsController: NSFetchedResultsController = { 
    // Initialize Fetch Request 
    let fetchRequest = NSFetchRequest(entityName: "Item") 

    // Add Sort Descriptors 
    let sortDescriptor = NSSortDescriptor(key: "createdAt", ascending: true) 
    fetchRequest.sortDescriptors = [sortDescriptor] 

    // Here you can set the limit of Fetch Using FetchRequest property 

    // Initialize Fetched Results Controller 
    let fetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: self.managedObjectContext, sectionNameKeyPath: nil, cacheName: nil) 

    // Configure Fetched Results Controller 
    fetchedResultsController.delegate = self 

    return fetchedResultsController 
}() 

.hファイルでプロパティを1つ作成

@property(強い、非アトミック)NSFetchedResultsController * fetchedResultsController。今までに新しいデータがcoreDataに保存されたときの.mで

この

- (NSFetchedResultsController *)fetchedResultsController 
{ 
    if (_fetchedResultsController != nil) { 
     return _fetchedResultsController; 
    } 


    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; 

    NSEntityDescription *entity = [NSEntityDescription entityForName:@"EntityName" inManagedObjectContext:self.managedObjectContext]; 
    [fetchRequest setEntity:entity]; 
    [fetchRequest setFetchBatchSize:20]; 

    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES]; 

    NSArray *sortDescriptors = [NSArray arrayWithObjects:sortDescriptor, nil]; 

    [fetchRequest setSortDescriptors:sortDescriptors]; 

    NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:@"Master"]; 
    aFetchedResultsController.delegate = self; 
    _fetchedResultsController = aFetchedResultsController; 

    NSArray *result = [self.managedObjectContext executeFetchRequest:fetchRequest error:nil]; 
    NSLog(@"Result: %@", result); 

    NSError *error = nil; 
    if (![_fetchedResultsController performFetch:&error]) { 
     NSLog(@"Unresolved error %@, %@", error, [error userInfo]); 
     abort(); 
    } 

    return _fetchedResultsController; 
} 

を実装し、それが自動的にNFCデリゲートMetodsの助けを借りて、テーブルビューに反映されます。

// MARK: Fetched Results Controller Delegate Methods 
func controllerWillChangeContent(controller: NSFetchedResultsController) { 
    tableView.beginUpdates() 
} 

//これは、すべての変更がセル内で発生した後に呼び出されます。

func controllerDidChangeContent(controller: NSFetchedResultsController) { 
    tableView.endUpdates() 
} 

//あなたがセルにそれのthats

func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath?, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath?) { 
    switch (type) { 
    case .Insert: 
     if let indexPath = newIndexPath { 
      tableView.insertRowsAtIndexPaths([indexPath], withRowAnimation: .Fade) 
     } 
     break; 
    case .Delete: 
     if let indexPath = indexPath { 
      tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade) 
     } 
     break; 
    case .Update: 
     if let indexPath = indexPath { 
      let cell = tableView.cellForRowAtIndexPath(indexPath) as! ToDoCell 
      configureCell(cell, atIndexPath: indexPath) 
     } 
     break; 
    case .Move: 
     if let indexPath = indexPath { 
      tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade) 
     } 

     if let newIndexPath = newIndexPath { 
      tableView.insertRowsAtIndexPaths([newIndexPath], withRowAnimation: .Fade) 
     } 
     break; 
    } 
} 

を削除したり、挿入したり、移動する時はいつでもこれはたびに呼び出されます。これがあなたを助けることを願っています。

このリンクを参照してください。 https://www.hackingwithswift.com/read/38/10/optimizing-core-data-performance-using-nsfetchedresultscontrolle

+0

ありがとうございます...同じ目的のための任意の目的のCコードはありますか? – Roger

+0

ええ、私は分であなたを編集します。 – karthik

+0

お兄さんありがとうございます – Roger

関連する問題