Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/96.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何删除UICollectionView单元格中的单个对象?_Ios_Objective C_Core Data_Uicollectionview_Uicollectionviewcell - Fatal编程技术网

Ios 如何删除UICollectionView单元格中的单个对象?

Ios 如何删除UICollectionView单元格中的单个对象?,ios,objective-c,core-data,uicollectionview,uicollectionviewcell,Ios,Objective C,Core Data,Uicollectionview,Uicollectionviewcell,我正在使用UICollectionView,通过此方法,我可以删除添加到CoreData的所有对象: - (IBAction)btnDelete:(id)sender { UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Are you sure?" message:@"Delete all favorites?" delegate:self cancelButtonTitle:@"Cancel" otherBut

我正在使用UICollectionView,通过此方法,我可以删除添加到CoreData的所有对象:

- (IBAction)btnDelete:(id)sender {
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Are you sure?" message:@"Delete all favorites?" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];
    [alertView show];
}

- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex {
    if(buttonIndex == 0){
        nil;
    } else if (buttonIndex == 1){
        AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
        NSManagedObjectContext *context = [delegate managedObjectContext];
        NSFetchRequest *execQuery =[[NSFetchRequest alloc] init];
        NSEntityDescription *descOggetto = [NSEntityDescription entityForName:@"HexCode" inManagedObjectContext:context];
        [execQuery setEntity:descOggetto];
        NSError *error = nil;
        _arr = [context executeFetchRequest:execQuery error:&error];

        for (NSManagedObject *ogg in _arr){
            [context deleteObject:ogg];
            [_favoriteCollectionView reloadData];
        }

        NSError *saveError = nil;
        [context save:&saveError];

        // NSLog(@"Delete");
    }
}

如何删除单个对象而不是全部对象?

您没有显示足够的代码,我无法给出具体的答案。但总的轮廓是这样的:

NSIndexPath *indexPath = ...;
NSManagedObject *obj = [self.fetchedResultsController objectAtIndexPath:indexPath];
[obj.managedObjectContext deleteObject:obj];
NSError *saveError = nil
[obj.managedObjectContext save:saveError];
...// error handling    
首先,需要获取与要删除的托管对象关联的单元格的索引路径。如何做到这一点取决于您的用例:

NSIndexPath *indexPath = ...;
从那里,从数据模型中获取托管对象。如果数据模型是可变数组(通常在集合视图中只有一个节时):

然后删除该对象并将其从阵列中删除:

[obj.managedObjectContext deleteObject:obj];
NSError *saveError = nil
[obj.managedObjectContext save:saveError];
...// error handling
[self.myDataModel removeObject:obj];
最后,通知集合视图您已删除一个项目,以便它可以更新显示:

[self.collectionView deleteItemsAtIndexPaths:@[indexPath]];
或者,如果您不想要动画:

[self.collectionView reloadData];
如果您使用的是
NSFetchedResultsController
,则答案会略有不同。如中所述,上述一些步骤将在您的
NSFetchedResultsControllerDelegate
实现中完成。其余步骤如下所示:

NSIndexPath *indexPath = ...;
NSManagedObject *obj = [self.fetchedResultsController objectAtIndexPath:indexPath];
[obj.managedObjectContext deleteObject:obj];
NSError *saveError = nil
[obj.managedObjectContext save:saveError];
...// error handling    

视情况而定。要删除哪个对象?如果使用以下命令:[context DeleteObject:ogg]删除CoreData中的所有对象,我只想删除单元格中包含的对象。否。在您发布的代码中,您将获取所有对象,对它们进行迭代,然后一次删除一个对象。命令
[context deleteObject:ogg]
只删除一个对象。当您编写:self.myDataModel时,这是什么意思?它是一个引用您的数据模型的假设属性。这一行的目的是说明您需要从数据模型中检索给定索引路径的对象。好的,谢谢。我按照你的建议解决了这个问题。