2012-02-01 64 views
0

我有一個包含多個節的tableView。這些部分是從'items'數組中填充的,它實際上將兩個不同的提取請求合併到單個字典數組中。第一個提取請求會拉取'未收集'的項目,第二個'收集'項目。使用核心數據和陣列從TableView插入和刪除行

表視圖填充罰款​​,我可以很容易地添加/刪除項目。我也可以選擇未收集的細胞的附件,它將移動到「收集」部分。我在做任何數據更改之後使用reloadData完成所有這些工作,現在正試圖將其轉換爲動畫插入/刪除。我從「未收集」到「收集」的轉變開始,這應該導致從前一節刪除一個單元並將其插入後一節。

我對事物的順序有點困惑。據我瞭解我應該:

  1. 更新的項目(從收集== NO爲YES開關)
  2. 救我的管理對象上下文
  3. 的「項目」點陣驅動表,現在更新automaticall
  4. 插入/刪除行

我發現我的錯誤:

'NSInternalInconsistencyException', reason: 'Invalid table view update. The application has requested an update to the table view that is inconsistent with the state provided by the data source. 

tableView在TableViewController中,它是表視圖的委託。我認爲我已經實現了numberOfSections,numberOfRows等。在這些我從我的'項目'數組返回計數。

我的代碼如下。

- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath 
{ 
    Item *item = [self itemForIndexPath:indexPath]; 

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
    UIButton *button = (UIButton *)cell.accessoryView; 

    if (![item collected]) { 
     //update item 
     [item setCollected:YES]; 
     [item setDateCollected:[NSDate date]]; 

     NSError *error = nil; 
     if (![item.managedObjectContext save:&error]) { 
      NSLog(@"Error saving collected/uncollected items"); 
     } 

     //update cell 
     [button setBackgroundImage:[UIImage imageNamed:@"checked.png"] forState:UIControlStateNormal]; 

     //update tableview 
     [tableView beginUpdates]; 
     [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
     [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:0 inSection:self.items.count]] withRowAnimation:UITableViewRowAnimationFade]; 

[tableView endUpdates]; 
    } 
    else if ([item collected]){...do the opposite 

回答

0

更換

[NSIndexPath indexPathForRow:0 inSection:self.items.count] 

[NSIndexPath indexPathForRow:self.items.count inSection:0] 

+0

不,我試圖將項目插入最後一節的最上面一行(這始終是我收集的項目) – 2012-02-03 20:30:27