2014-09-12 70 views
1

確定更新一個UICollectionView,我真的卡住位置:/在蘋果的集合視圖編程指南插入它說,一個項目有兩個步驟:,當一個項目被添加(核心數據),停留在insertItemsAtIndexPaths

  1. 更新數據源對象中的數據。
  2. 調用收集視圖的相應方法來插入或刪除節或項目。

我被困在第二個。我希望新項目出現在集合視圖的開始處。我不知道要爲insertItemsAtIndexPaths:參數放置什麼。我知道它需要一個索引對象數組,但我不知道如何告訴它選擇集合的開始,因爲它是索引值,並且我也很困惑如何告訴它其他的東西。你可以在我之前的代碼中看到(cellForItemAtIndexPath)我在填充集合時修改了單元內按鈕元素的屬性。如果我想在創建時修改新項目,我該怎麼做?我非常感謝這方面的任何幫助,我覺得我在這裏錯過了很多,而且真的碰到了一堵磚牆。

@implementation RepsCollectionViewController 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
     // Custom initialization 
    } 
    return self; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    NSError *error = nil; 
    if (![self.fetchedResultsController performFetch:&error]) { 
     NSLog(@"Error : %@", error); 
     abort(); 
    } 
} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

#pragma mark - Collection view data source 

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { 
    id <NSFetchedResultsSectionInfo> secInfo = [self.fetchedResultsController.sections objectAtIndex:section]; 

    return [secInfo numberOfObjects]; 
} 

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { 

    RepCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath]; 

    Rep *rep = [self.fetchedResultsController objectAtIndexPath:indexPath]; 

    cell.repButton.pressed = [rep.completed boolValue]; 
    cell.repButton.indexPath = indexPath; 

    return cell; 
} 

#pragma mark - Fetched Results Controller 

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

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc]init]; 
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Rep" inManagedObjectContext:self.managedObjectContext]; 
    [fetchRequest setEntity:entity]; 

    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc]initWithKey:@"dateTimeCreated" ascending:NO]; 
    [fetchRequest setSortDescriptors:[NSArray arrayWithObjects:sortDescriptor, nil]]; 

    _fetchedResultsController = [[NSFetchedResultsController alloc]initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:nil]; 

    _fetchedResultsController.delegate = self; 

    return _fetchedResultsController; 
} 

#pragma mark - IBActions 

- (IBAction)addRep:(id)sender { 

    [NSEntityDescription insertNewObjectForEntityForName:@"Rep" inManagedObjectContext:self.managedObjectContext]; 

    [self.collectionView performBatchUpdates:^{ 

     [self.collectionView insertItemsAtIndexPaths:WHAT TO PUT HERE?]; 

    } completion:nil]; 
} 

回答

2

你似乎並沒有把執行任何NSFetchedResultsControllerDelegate的方法。對於您想要的功能,作爲獲取結果控制器的代表是關鍵。

NSFetchedResultsController的一個實例將通知它的委託對與其獲取請求匹配的對象集合所做的更改。它通過調用代理上的各種NSFetchedResultsControllerDelegate方法來實現。例如,如果一個新的對象被插入到分配給所提取的結果控制器的讀取請求相匹配的NSManagedObjectContext,以下NSFetchedResultsControllerDelegate方法稱爲:

controllerWillChangeContent:

controller:didChangeObject:atIndexPath:forChangeType:newIndexPath:

controllerDidChangeContent:

controller:didChangeObject:atIndexPath:forChangeType:newIndexPath:是您可以在集合視圖上調用insertItemsAtIndexPaths:方法的地方,告訴它在其數據源中有新項目可用。這看起來像:

- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath { 
    UICollectionView *collection = self.collectionView; 
    switch(type) { 
       case NSFetchedResultsChangeInsert: 
       [collection insertItemsAtIndexPaths:@[newIndexPath] ]; 
       break; 
    } 
} 

顯然,一個真實的例子要正確處理其他類型的變化,這只是一個說明性的例子。您還應該實施其他NSFetchedResultsControllerDelegate方法。使用批量更新進行收集視圖更改不是必需的,並且可以輕鬆實現。

+0

例如,如果添加了多個對象,則會導致崩潰。 – 2014-09-13 11:17:20

+0

冷靜,非常感謝你花時間給出這樣一個詳細的答案。我試過了 - 只是爲了NSFetchedResultsChangeInsert選項 - 它的功能就像一個魅力!我將在週末後添加所有委託方法的其餘要求,並將在未來使用此方法更新集合視圖。不夠感謝你! – Jared 2014-09-13 13:34:28

-1

我也沒有正確理解你的問題。但是,這會幫助你,

從核心數據獲取的所有數據後,

[self.collectionView reloadData]; 

在此之前,把所有的數據self.fetchedResultsController

+0

嗨BC_Dilum,當我嘗試的時候,應用程序在觸發操作時崩潰:「無效的更新:部分0中的項目數目無效。 – Jared 2014-09-12 20:25:00