1

我有一个 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???             

    }
}

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

4

2 回答 2

0

如果单元格选择立即触发 segue,您可以使用获取所需的方法indexPathForSelectedItems来获取.UICollectionViewindexPathNSManagedObject

尝试这个:

- (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;            
    }
}
于 2013-05-19T03:23:52.450 回答
0

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

@property (weak,nonatomic) NSManagedObject* myObject;

然后你可以为这个单元格分配这个属性 NSManagedObject cellForItemAtIndexPath

- (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*/            
                }
        }
于 2013-05-19T04:50:42.003 回答