【发布时间】:2013-10-16 21:09:08
【问题描述】:
我正在使用 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");
}
}
如何删除单个对象而不是全部?
【问题讨论】:
-
视情况而定。您要删除哪个对象?
-
如果你使用这个命令:[context DeleteObject: ogg] 删除CoreData中的所有对象,而我只想删除单元格中包含的对象。
-
没有。在您发布的代码中,您正在获取所有对象,遍历它们,并一次删除它们。命令
[context deleteObject:ogg]只删除一个对象。
标签: ios objective-c core-data uicollectionview uicollectionviewcell