7

我很難在tableView中實現2 nsfetchedresultsController。我在表(2段)的每個部分想要一個fetchedresultsController,是這樣的:
UItableView上的多個NsfetchedResultsController

收藏

  • 產品
  • 產品2


  • 產品3

我知道,在這個例子中,我不會需要2 nsfetchedResultsController但後來我需要..

當我刪除從一個nsfetchedResultsController把它傳遞給其他的產品,我得到這個錯誤:

Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (0) must be equal to the number of rows contained in that section before the update (0), plus or minus the number of rows inserted or deleted from that section (1 inserted, 0 deleted). with userInfo (null)"

我認爲,這是因爲當第一fetchedRController檢測到變化它的第二個被稱爲是正確的之前更新的tableView?我應該使用NSMutableArrays並手動更新tableView嗎?

代碼:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
NSInteger numberOfRows = 0; 

switch (section) 
{ 
    case WISHLIST_SECTION: 

    { 
     numberOfRows =[[fetchedResultsControllerwish fetchedObjects]count]; 


    }; 
     break; 

    case PURCHASED_SECTION: 
    { 
     numberOfRows =[[fetchedResultsControllerPurchased fetchedObjects]count]; 

    }; 
     break; 


    default: 
     break; 
} 
return numberOfRows; 
} 

- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller { 
// The fetch controller is about to start sending change notifications, so prepare the table view for updates. 
[self.tableView beginUpdates]; 
} 


- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller 
{ 

// The fetch controller has sent all current change notifications, so tell the table view to process all updates. 
[self.tableView endUpdates]; 
} 

- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject 
    atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type 
    newIndexPath:(NSIndexPath *)newIndexPath { 

UITableView *tableViewT = self.tableView; 

switch(type) { 

    case NSFetchedResultsChangeInsert: 
     [tableViewT insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] 
         withRowAnimation:UITableViewRowAnimationFade]; 
     break; 

    case NSFetchedResultsChangeDelete: 
     [tableViewT deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] 
         withRowAnimation:UITableViewRowAnimationFade]; 
     break; 

    case NSFetchedResultsChangeUpdate: 
     [self configureCell:(GiftEventTableViewCell *)[tableViewT cellForRowAtIndexPath:indexPath] atIndexPath:indexPath]; 
     break; 

    case NSFetchedResultsChangeMove: 
     [tableViewT deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] 
         withRowAnimation:UITableViewRowAnimationFade]; 
     [tableViewT insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] 
         withRowAnimation:UITableViewRowAnimationFade]; 
     break; 
} 
} 

-(void)configureCell:(GiftEventTableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath 
{ 
switch (indexPath.section) { 
    case WISHLIST_SECTION: 
    { 
     GiftEvent *giftEv=(GiftEvent *)[fetchedResultsControllerwish objectAtIndexPath:indexPath]; 
     cell.gift=giftEv; 

    }; 
     break; 
    case PURCHASED_SECTION: 
    { 
     GiftEvent *giftEv=(GiftEvent *)[fetchedResultsControllerPurchased objectAtIndexPath:indexPath]; 

     //if i don't use indexpath.row in the second section i get an out of bounds error 
     //GiftEvent *giftEv=(GiftEvent *)[[fetchedResultsControllerPurchased fetchedObjects]objectAtIndex: indexPath.row]; 
     cell.gift=giftEv; 

    }; 
     break; 




    default: 
     break; 
} 

} 

// Override to support editing the table view. 
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { 
if (editingStyle == UITableViewCellEditingStyleDelete) 
{ 
    switch (indexPath.section) { 
     case WISHLIST_SECTION: 
     { 
      // Delete the managed object for the given index path 

      [self.managedObjectContext deleteObject:[fetchedResultsControllerwish objectAtIndexPath:indexPath]]; 

      // Save the context. 
      NSError *error; 
      if (![self.managedObjectContext save:&error]) { 
       /* 
       Replace this implementation with code to handle the error appropriately. 

       abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. If it is not possible to recover from the error, display an alert panel that instructs the user to quit the application by pressing the Home button. 
       */ 
       NSLog(@"Unresolved error %@, %@", error, [error userInfo]); 
       abort(); 
      } 
      [self.tableView reloadData]; 

      }; 
      break; 
     case PURCHASED_SECTION: 
     { 
      // Delete the managed object for the given index path 

      [self.managedObjectContext deleteObject:[fetchedResultsControllerPurchased objectAtIndexPath:indexPath]]; 

      // Save the context. 
      NSError *error; 
      if (![self.managedObjectContext save:&error]) { 
       /* 
       Replace this implementation with code to handle the error appropriately. 

       abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. If it is not possible to recover from the error, display an alert panel that instructs the user to quit the application by pressing the Home button. 
       */ 
       NSLog(@"Unresolved error %@, %@", error, [error userInfo]); 
       abort(); 
      } 

     }; 
      break; 

     default: 
      break; 
    } 
} 
} 

回答

1

:)對不起,這是一個noob錯誤..錯誤發生的原因是indexPath.section返回tableView中的部分,但我沒有在fetchedResultsController(總是索引0)的部分。編輯:實際上有一個..

NSIndexPath *indexPathTemp = [NSIndexPath indexPathForRow:indexPath.row inSection:0]; 

不知道是否是最好的辦法,但解決了問題。
Thks!
Ricardo Castro

1

使用倍數是錯誤的方法。正確的解決方案是使用sectionNameKeyPath參數NSFetchedResultController將結果分成多個部分,在您的情況下,它將是產品上購買的布爾屬性。並且您還應該按升序將它作爲第一個排序描述符,因爲您想要購買以上未購買的產品。使用標題標題表委託方法爲每個部分索引返回字符串「Wishlist」和「Purchased」。

如果您認爲將來可能有第三個列表,那麼您最好使用數字狀態屬性,並且在代碼中爲產品可能處於的每個狀態(或列表)列舉枚舉。這是完成而不是排序一個布爾。

相關問題