【问题标题】:How do I utilize CoreData when moving from collection view to detail view从集合视图移动到详细视图时如何利用 CoreData
【发布时间】:2013-05-19 03:03:33
【问题描述】:

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

当一个单元格被选中时,我试图从集合视图移动到详细视图。 CoreData 实体有一个“摘要”属性,我想在详细视图中与图像一起显示。

我知道我可以通过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???             

    }
}

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

【问题讨论】:

    标签: iphone ios core-data segue managedobjectcontext


    【解决方案1】:

    如果单元格选择立即触发segue,你可以利用UICollectionViewindexPathForSelectedItems方法获取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;            
        }
    }
    

    【讨论】:

    • 谢谢西蒙!这行得通!我还发现 Master-Detail 模板的 prepareForSegue 方法中的代码也可以正常工作。我不知道为什么在我问这个问题之前我没有尝试过!我更感谢您的回答,因为现在我理解了 indexPathForSelectedItems 方法!再次感谢!
    【解决方案2】:

    您可以向您添加属性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*/            
                    }
            }
    

    【讨论】:

    • 伊拉,感谢您的回复。我能够让我的代码在 Simons 上首先回答,但我相信查看你的代码我也可以这样做。我还没有证明这一点,但它似乎是合乎逻辑的并且是该问题的可靠解决方案。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2013-08-10
    • 2014-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-08
    相关资源
    最近更新 更多