2011-05-06 77 views
1

我有一個由fetchedResultsController支持的表視圖。在我的表格單元格中,我有一個執行軟刪除的按鈕,就視圖而言...點擊按鈕,執行軟刪除的選擇器被調用,並且...應該發生某事(I'我嘗試了很多東西),但沒有嘗試過像Apple的行動畫那樣的動畫。核心數據/ UITableView上的動畫數據刷新(軟刪除)

我可以執行簡單的行動畫,就像使其滑動一樣(在我告訴表重新加載之後會留下一個空行),這反過來又有點刺耳的效果)。這是我來最接近:

 -(void) completeTask: (id) sender { 
    NSIndexPath *indexPath = [self.tableView indexPathForCell:(UITableViewCell *)[[sender superview] superview]]; 

    NSDate *date = [[NSDate alloc] init]; 
    NSManagedObject *task = [self.fetchedResultsController objectAtIndexPath:indexPath]; 

    [task setValue:date forKey:@"complete"]; 

    AppController *ac = [AppController sharedAppController]; 
    NSManagedObjectContext *moc = [ac managedObjectContext]; 

    NSError *error = nil; 
    if(![moc save:&error]){ 
     NSLog(@"%@ %@",error, [error description]); 
     exit(-1); 
    } else { 
     UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath]; 

     CGRect cellFrame = cell.frame; 
     [UIView beginAnimations:nil context:nil]; 
     [UIView setAnimationDuration:0.35]; 
     [UIView setAnimationDelegate:self]; 
     [UIView setAnimationDidStopSelector:@selector(someAnimationDidStop:finished:context:)]; 
     cell.frame = CGRectMake(cellFrame.size.width , cellFrame.origin.y, cellFrame.size.width, cellFrame.size.height); 
     [UIView commitAnimations]; 
    } 
} 

-(void) someAnimationDidStop:(NSString *)animationID 
        finished:(BOOL)finished context:(void *)duration { 
    NSLog(@"Animation Stopped"); 
    [self fetchResults]; // this guy performs a new fetch and table data reload 
} 

我覺得我在正確的軌道上是有點,但我不認爲這真的是相當的答案。我希望以某種方式controller:didChangeObject:atIndexPath:forChangeType:newIndexPath:將是答案。我的猜測是,如果我需要股票Apple動畫,我將不得不創建一個單獨的NSMutableArray來跟蹤結果,並確保它保持同步。

思想和觀點?

謝謝!

回答

1

我想你已經爲NSFetchedResultsController不取的「軟刪除」對象的謂詞。

所以理論上你所要做的就是實施NSFetchResultsControllerDelegate方法的全部。文檔中的示例實現將正常工作。
不要忘記設置的NSFetchedResultsController

delegate然後代替手工管理的細胞,只是改變從對象的「軟刪除」值。NSFetchResultsController將監視上下文,它會發現這種更改會使對象從集合中消失,因此它將從tableView中刪除單元格。

+0

你是對的 - 在對我的委託方法進行一些更改以正確啓用真正的刪除後,看起來我的答案在下面停止工作,直到我刪除了手動表管理。謝謝! – Cameron 2011-05-09 19:38:05

1

編輯:@fluchtpunkt答案是正確的。在對代碼進行更多更新之後,我的解決方案再次崩潰。刪除重新獲取FRC的塊使其全部重新工作。


我很驚訝我在昨天的所有迭代測試(學習)中都沒有弄清楚這一點。我遇到了以核心數據抓取的結果與表視圖同步的很好的方式遇到的問題 - 通常我會得到錯誤匹配索引(等等等等)(行數(#)必須等於#加號或減去行數等等等等等等(0插入:#刪除)..等)。

我的步驟來完成此如下:

  1. 執行軟刪除&保存
  2. 獲取的成果控制器設置爲nil self.fetchedrResultsController = nil
  3. 做一個新的獲取
  4. 然後刪除該行來自表格:
   self.fetchedResultsController = nil; 
     NSError *error = nil; 
     if (![[self fetchedResultsController] performFetch:&error]) { 
      NSLog(@"Unresolved error %@, %@", error, [error userInfo]); 
      abort(); 
     } else { 
      [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationRight]; 
     } 

我真的不知道爲什麼我以前沒有弄清楚這一點,但它非常有意義。新鮮的眼睛?

這裏的全貌:

 
-(void) completeTask: (id) sender { 

    NSIndexPath *indexPath = [self.tableView indexPathForCell:(UITableViewCell *)[[sender superview] superview]]; 

    NSManagedObject *task = [self.fetchedResultsController objectAtIndexPath:indexPath]; 

    [task setValue:date forKey:@"complete"]; 

    AppController *ac = [AppController sharedAppController]; 
    NSManagedObjectContext *moc = [ac managedObjectContext]; 

    NSError *error = nil; 
    if(![moc save:&error]){ 
     NSLog(@"Uh oh - couldn't save the delete! %@ \n%@",error, [error description]); 
     exit(-1); 
    } else { 

     // Save successful; re-fetch data (background) 
     self.fetchedResultsController = nil; 
     NSError *error = nil; 
     if (![[self fetchedResultsController] performFetch:&error]) { 
      NSLog(@"Unresolved error %@, %@", error, [error userInfo]); 
      abort(); 
     } else { 
      //update the table view with row animation 
      [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] 
            withRowAnimation:UITableViewRowAnimationRight]; 
     } 
    } 
}