回答

1

它看起來好像你是以錯誤的方式去做這件事。如果我是你,我會嘗試刪除該對象,然後用正確的部分鍵重新插入它。因此,如果你的NSManagedObject的section-key爲1,你將需要在本地存儲對象值,同時刪除對象。然後,您可以使用存儲的值創建新記錄,並將部分關鍵字更新爲0.

這應該刪除舊的單元格並在新節中插入一個新單元格。

見下文 -

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSManagedObject *obj = [_fetchedResultsController objectAtIndexPath:indexPath]; 

    NSString *name = obj.name; 

    [self.managedObjectContext deleteObject:task]; 

    NSError *error = nil; 
    [self.managedObjectContext save:&error]; 

    NSManagedObject *newObj = [NSEntityDescription insertNewObjectForEntityForName:@"entityName" inManagedObjectContext:self.managedObjectContext]; 


    [newObj setValue:name forKey:@"name"]; 

    [self.managedObjectContext newObj]; 

    error = nil; 
    [self.managedObjectContext save:&error]; 
} 
+0

感謝這個似乎一個明顯的路要走,它的工作好! –

+1

這對我來說似乎不是一個好的解決方案。如果你有一個使用'sectionNameKeyPath:'參數的結果控制器將結果分組爲段,那麼像[[obj setValue:@(newSectionNumber)forKey:@「sectionNumber」]'應該足以將對象移動到不同部分自動。 –

相關問題