从集合视图移动到详细视图时,如何使用CoreData

时间:2013-05-19 03:03:34

标签: iphone ios core-data segue managedobjectcontext

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

我正在尝试在选择单元格时从集合视图移动到详细视图。我希望在详细视图中显示CoreData实体的“summary”属性以及图像。

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

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

以下是我的CollectionView的填充方式。

- (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???             

    }
}

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

2 个答案:

答案 0 :(得分:0)

如果单元格选择立即触发segue,您可以使用indexPathForSelectedItems UICollectionView方法来获取获取indexPath所需的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;            
    }
}

答案 1 :(得分:0)

您可以向MyNewsCollectionViewCell添加一个属性,如下所示:

@property (weak,nonatomic) NSManagedObject* myObject;

然后,您可以在cellForItemAtIndexPath中为此单元格分配此属性NSManagedObject。

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
        /* Add this line to All that u r already doing*/   
        [cell setMyObject:object];
        return cell;
    }

现在在didSelectCellMethod,您可以致电[self performSegueWithIdentifier: @"MySegue" sender: cell];

然后在prepareForSegue:中从发件人处获取对象。

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(MyNewsCollectionViewCell)sender
            {
                if ([[segue identifier] isEqualToString:@"newsDetail"]) {
                    NewsDetailViewController *vc =[segue destinationViewController];
                    NSManagedObject* object = sender.myObject;

                    //use this object to set values of 

                    vc.newsDetailImageView = /*set value*/
                    vc.newsDetailText = /*set value*/            
                }
        }