2011-04-04 62 views
0

在上連接到NSFetchedResultsController一個的tableView的CELL(不是部分)刪除操作的響應我獲得這個錯誤:NSFetchedResultsControllerDelegate和deleteRowsAtIndexPaths:部分問題

「無效更新:的 部分無效數。包含在表視圖 更新後段 的數目(1)必須等於包含在表部分的數量 更新(2)之前 視圖,加上或 減去區間的數目插入 或刪除(0插入,0刪除)。'

我知道問題與更新之前和之前的部分編號有關。它說我沒有刪除部分,但後值與前值不同。

好吧,這是真的!但我的部分依賴於行單元格,因此如果我刪除某個部分的最後一行單元格,則該部分消失。

在這裏,我如何定義部分和行號: 創建部分以按屬性「日期」分組行。因此,如果一行具有屬性「日期」2010年4月10日和第二行有2010年4月11日,我有2節包含1行。

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    return [[self.controller sections]count]; 
} 


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    id <NSFetchedResultsSectionInfo> sectionInfo = [[self.controller sections] objectAtIndex:section]; 
    return [sectionInfo numberOfObjects]; 
} 

這裏我commitEditingStyle,在那裏我從表中刪除單元格,並從數據庫中刪除數據的定義(對象類型「交易」是的NSManagedObjectContext的子類,定義了我的模型)。

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { 

    if (editingStyle == UITableViewCellEditingStyleDelete) { 

     Transactions *trans = (Transactions *)[self.controller objectAtIndexPath:indexPath]; 

     //Delete transaction 
     [self.context deleteObject:trans]; 

     NSError *error = nil; 
     [self.context save:&error]; 
     [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationTop]; 
    } 
} 

回答

0

我發現問題在哪裏。 委託在從UITabBar調用的控制器中定義。每次出現它的視圖時,都會調用一個函數來獲取NSFEtchedResultController上的數據(帶有performFetch)併爲NSFetchedResultControllerDelegate設置委託。這種方式(我不知道爲什麼)每次我嘗試刪除或插入表格上的單元格/部分時,委託似乎被重複儘可能多的時間,因爲我通過UITabBar打開控制器,因此委託函數被重複多次時間創造問題和我在問題中描述的錯誤。

爲了避免這個問題,現在我在ViewController的init函數中設置了Request和Delegate,並且沒有其他地方。這確保它只設置一個委託引用,並且只通過NSFetchResultController執行一個Fetch。