1

我有一个带有 UICollectionView 的应用程序,视图显示自定义 UICollectionViewCells。当其中一个单元格注册一个水龙头时,它会以模型视图控制器的形式打开一个详细视图。集合视图中填充了来自 Core Data 的数据,它看起来像这样:

#pragma mark - Get Items From CoreData
-(NSArray*)refreshTableData
{



NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Produkt"
                                          inManagedObjectContext:context];
[fetchRequest setEntity:entity];
NSError* error;
NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];

/*for (Produkt *produkt in fetchedObjects)
{
    NSLog(@"Name: %@", produkt.name);
}*/

return fetchedObjects;
}

 #pragma mark - UICollectionView Datasource
- (NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection:   (NSInteger)section {
return [[self refreshTableData] count];
}

 - (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath {
CollectionViewCell* cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ItemCell" forIndexPath:indexPath];

Produkt* produkt = [[self refreshTableData]objectAtIndex:indexPath.row];

[cell.collectionLabel setText:produkt.name];
[cell.collectionImage setImage:[[UIImage alloc]initWithData:produkt.image]];

[cell.layer setCornerRadius:10];
[cell.layer setMasksToBounds:YES];

CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = cell.bounds;
gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor blackColor] CGColor], (id)[[UIColor whiteColor] CGColor], nil];
[cell.layer insertSublayer:gradient atIndex:0];

return cell;
}

#pragma mark UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{

Produkt* produkt = [[self refreshTableData]objectAtIndex:indexPath.row];
produktView = [[ProduktViewController alloc]
               initWithNibName:@"ProduktViewController"
               bundle:nil];



produktView.modalPresentationStyle = UIModalPresentationFormSheet;
produktView.delegate = self;
produktView.produkt = produkt;

[self presentViewController:produktView animated:YES completion:nil];
}

- (UIEdgeInsets)collectionView:
(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
return UIEdgeInsetsMake(50, 20, 50, 20);
}

当我创建一个新的 Produkt 对象(NSManagedObject)时,保存它并重新加载 UICollectionView 一切正常。这就是创建新 Produkt-Object 的代码:

-(void)selectedItem:(NSString *)selectedItem
 {
//Dismiss the popover if it's showing.
if (itemPickerPopover) {
    [itemPickerPopover dismissPopoverAnimated:YES];
    itemPickerPopover = nil;
}

if([selectedItem isEqualToString:@"Produkt"])
{
    NSLog(@"SELECTED ITEM:%@",selectedItem);
    produktView = [[ProduktViewController alloc]
                       initWithNibName:@"ProduktViewController"
                       bundle:nil];
    Produkt* newProdukt = [NSEntityDescription
                           insertNewObjectForEntityForName:@"Produkt"
                           inManagedObjectContext:context];

    produktView.modalPresentationStyle = UIModalPresentationFormSheet;
    produktView.delegate = self;
    produktView.context = context;
    produktView.produkt = newProdukt;

    [self presentViewController:produktView animated:YES completion:nil];
}

}

现在,当我选择一个单元格并弹出详细视图时,我在详细视图中编辑数据,然后保存它,使用此功能(包含在详细视图类中):

   -(void)saveChanges
 {
        NSLog(@"SAVE SAVE SAVE SAVE SAVE SAVE SAVE SAVE SAVE SAVE SAVE ");

    produkt.name = name.text;
    produkt.artikelNr = artikelNr.text;
    produkt.gekauftBei = bestelltBei.text;
    produkt.gebrauchsAnweisung = gebrauchsAnleitung.text;

    NSData *savedImageData = UIImagePNGRepresentation(image.image);
    produkt.image = savedImageData;

    NSError *error;
    [context save:&error];

    }

当细节控制器关闭时,这个委托函数被调用:

#pragma mark ProduktView delegate
-(void)didDismissPresentedViewController
{
NSLog(@"DISMISS CONTROLLER");
[produktView dismissViewControllerAnimated:YES completion:nil];

[collectionView reloadData];
}

现在问题来了:当我保存更改的对象并重新加载表格视图时,显示的单元格与错误的实体相关联。例如,集合视图显示对象: 1 、 2 、 3 、 4 但是当我单击对象 3 时,详细视图显示对象 1。在我关闭视图控制器后,集合视图再次更改顺序。当我重新启动应用程序时,我对对象所做的更改不会保存。

在主视图控制器中,我创建了一个 NSMangedObjectContext,我将其传递给其他控制器,因此我始终处于相同的上下文中。

AppDelegate *appDelegate =
[[UIApplication sharedApplication] delegate];
context =
[appDelegate managedObjectContext];

谢谢你的帮助。

4

1 回答 1

2

如果没有排序描述符,则获取请求返回的对象的顺序是未定义的。甚至不能保证两个相同的 fetch 请求以相同的顺序返回对象。

由于您所有的集合视图数据源方法都执行一个新的获取请求(这是非常无效的!)关于哪个对象没有什么可以说的

Produkt* produkt = [[self refreshTableData]objectAtIndex:indexPath.row];

cellForItemAtIndexPath实际返回。

因此,一种可能的解决方案是将对象一次提取到 an 中NSArray并将该数组用作集合视图数据源。如果已添加或修改对象,则执行新的获取请求并重新加载集合视图。

另一种可能的解决方案是使用NSFetchedResultsController(FRC)。FRC 自动跟踪对托管对象上下文的更改,并在插入、删除或修改对象时调用委托函数。如果您选择该路径,您应该注意,就自动动画更新而言,将 FRC 与集合视图一起使用比使用表格视图更棘手。但是这篇文章http://ashfurrow.com/blog/uicollectionview-example可能会有所帮助。

于 2013-08-04T10:38:19.033 回答