UICollectionViewCell将数据发送到另一个viewController

时间:2014-12-30 10:40:13

标签: ios objective-c uicollectionview uicollectionviewcell

我有UICollectionView。我在这里展示了MainBundle的一些照片。这些照片的位置或目录保存在SQLite数据库中。我正在使用segue。问题是当我尝试将图像链接发送到detailsViewController时,它没有发送链接。

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    selectedPhotoIndex = indexPath.row;
}

-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{   
    if ([segue.identifier isEqualToString:@"PhotoDetailsViewController"])
    {
        PhotoDetailsViewController *photoDetailsViewController = [segue destinationViewController];
        PhotoProperty *pProperty = [self.arrayOfPhotos objectAtIndex:selectedPhotoIndex];
        [photoDetailsViewController setImagePath:pProperty.link];
        //selectedPhotoIndex = 0;
    }
}

但如果我将代码放在segue.identifier之外,则会将链接发送到detailsViewController

-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{   
    PhotoDetailsViewController *photoDetailsViewController = [segue destinationViewController];
        PhotoProperty *pProperty = [self.arrayOfPhotos objectAtIndex:selectedPhotoIndex];
        [photoDetailsViewController setImagePath:pProperty.link];
        //selectedPhotoIndex = 0;
}

但问题在于,它保留了前一个链接的参考位置,并显示正确的图像,我必须再次点击选定的拇指线图像。我试着用这些替代品但没有运气。

-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"PhotoDetailsViewController"])
    {
        PhotoDetailsViewController *photoDetailsViewController = [segue destinationViewController];
        CustomCell *cell = (CustomCell *) sender;
        NSIndexPath *indexPath = [self.photoCollectionView indexPathForCell:cell];

        //NSLog(@"indexPath %@",indexPath);
        PhotoProperty *pProperty = [self.arrayOfPhotos objectAtIndex:indexPath.row];
        [photoDetailsViewController setImagePath:pProperty.link];
    }
}


-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"PhotoDetailsViewController"])
    {
        CustomCell *cell = (CustomCell *) sender;
        NSIndexPath *indexPath = [self.photoCollectionView indexPathForCell:cell];

        int num = indexPath.row %10;
        NSLog(@"num %i", num);

        PhotoDetailsViewController *photoDetailsViewController = (PhotoDetailsViewController *)[segue destinationViewController];
        PhotoProperty *pProperty = [self.arrayOfPhotos objectAtIndex:num];
        [photoDetailsViewController setImagePath:pProperty.link];
    }
}

这是我的完整代码:

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.sQLite = [[SQLite alloc] init];
    [self.sQLite callDataBaseAndPhotoTableMethods];

    self.arrayOfPhotos = [[NSMutableArray alloc] init];
    self.arrayOfPhotos = [self.sQLite returnDataFromPhotoTable];

    self.photoCollectionView.delegate = self;
    self.photoCollectionView.dataSource = self;
}

/*------------ It takes the original size of photo and make it's size smaller (Down)------------*/
- (UIImage*)imageWithImage:(UIImage*)image scaledToSize:(CGSize)newSize;
{
    UIGraphicsBeginImageContext(newSize);
    [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return newImage;
}
/*------------ It takes the original size of photo and make it's size smaller (Down)------------*/

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return [self.arrayOfPhotos count];
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CustomCell *cell = [self.photoCollectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
    self.photoProperty = [self.arrayOfPhotos objectAtIndex:indexPath.row];

    // NSLog(@"id %d", photoProperty.idNumPHOTO);
    // NSLog(@"name %@", photoProperty.name);
    // NSLog(@"links %@", photoProperty.link);

    NSData *imageData = [NSData dataWithContentsOfFile:photoProperty.link];
    UIImage *originalImage = [[UIImage alloc] initWithData:imageData];

    // This is the size where I get from the Cell UIImageView hight & wodth
    CGSize size = CGSizeMake(36.0f, 51.0f);
    UIImage *thumbNailImage = [self imageWithImage:originalImage scaledToSize:size];

    cell.imageView.image = thumbNailImage;

    cell.layer.shouldRasterize = YES;
    cell.layer.rasterizationScale = [UIScreen mainScreen].scale;

    return cell;
}

如果有人了解我的问题并有任何解决方案,请与我分享。非常感谢先进 祝你有愉快的一天。

2 个答案:

答案 0 :(得分:1)

问题是您使用的是segue的错误属性。 identifier用于performSegueWithIdentifier:。您使用destinationViewController来获取目的地,并按照[segue.destinationViewController isKindOfClass:NSClassFromString(@"PhotoDetailsViewController")]

进行检查

答案 1 :(得分:0)

在目标视图控制器的viewDidAppear方法中检查imagePath。

将调用目标视图控制器的第一个viewDidLoad,然后将调用之前控制器的prepareForSegue。因此,如果您在目标视图控制器的viewDidLoad中使用imagePath,那么您将无法获得它的值。

希望这会有所帮助..