如何在从集合视图移动到详细视图时使用CoreData [英] How do I utilize CoreData when moving from collection view to detail view

查看:127
本文介绍了如何在从集合视图移动到详细视图时使用CoreData的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个IOS应用程序使用RestKit将json格式的数据从服务器拉到CoreData实体。实体的一些属性帮助填充具有每个单元格的图像和标题的集合视图。

I have an IOS app that is using RestKit to pull json formatted data from a server into a CoreData Entity. Some of the attributes of the entity help populate a collection view with an image and title for each cell.

我选择单元格时尝试从集合视图移动到详细视图。 CoreData实体的summary属性,我想与详细视图和图像一起显示。

I am attempting to move from the collection view to a detail view when a cell is selected. There is a "summary" attribute of the CoreData Entity that I would like to display in the detail view along with the image.

我知道我可以通过 prepareForSegue 方法传递数据。但我不知道如何指定要传递的图片和摘要数据

I know I can pass data thru the prepareForSegue method. But I am not sure how to specify the image and summary data I want to pass.

也许传递图片和摘要不是正确的方法?我应该将 managedObjectContext 传递到详细视图控制器并从那里获取结果吗?

Maybe passing the image and summary is not the proper way? Should I be passing the managedObjectContext to the detail view controller and fetching the results from there?

CollectionView已填充。

Here is how my CollectionView is populated.

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"Cell";

MyNewsCollectionViewCell *cell = (MyNewsCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
NSManagedObject *object = [self.fetchedResultsController objectAtIndexPath:indexPath];
NSURL *photoURL = [NSURL URLWithString:(NSString *) [object valueForKey:@"imageUrl"]];
NSData *photoData = [NSData dataWithContentsOfURL:photoURL];
[cell.cellimg setImage:[[UIImage alloc] initWithData:photoData]];        
[cell.title setText:[object valueForKey:@"title"]];      

return cell;

这是 prepareForSegue 方法

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"newsDetail"]) {

      NewsDetailViewController *vc =[segue destinationViewController];
      vc.newsDetailImageView = //How do I designate the image from the cell I selected???
      vc.newsDetailText = //How do I designate the text from the cell I selected???             

    }
}

这显然是一个初学者的问题。 ...任何帮助将非常感激。鉴于我是一个初学者基本示例代码真的帮助!

This is obviously a beginners question.... any help would be much appreciated. Given that I'm a beginner basic example code really helps!

推荐答案

如果单元格选择立即触发segue,可以使用 indexPathForSelectedItems 方法 UICollectionView 获取 indexPath ,您需要得到 NSManagedObject

If the cell selection triggers the segue instantly, you can make use of the indexPathForSelectedItems method of UICollectionView to get the indexPath that you need to get your NSManagedObject.

请尝试以下操作:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"newsDetail"]) {
        NewsDetailViewController *vc =[segue destinationViewController];
        // In the following line, replace self.collectionView with your UICollectionView
        NSIndexPath *indexPath = [[self.collectionView indexPathsForSelectedItems] objectAtIndex:0];

        NSManagedObject *object = [self.fetchedResultsController objectAtIndexPath:indexPath];
        NSURL *photoURL = [NSURL URLWithString:(NSString *)[object valueForKey:@"imageUrl"]];
        NSData *photoData = [NSData dataWithContentsOfURL:photoURL];
        UIImage *img = [[UIImage alloc] initWithData:photoData];

        vc.newsDetailImageView = [[UIImageView alloc] initWithImage:img];
        vc.newsDetailText = howeverYouGetYourObjectSummary;            
    }
}

这篇关于如何在从集合视图移动到详细视图时使用CoreData的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆