2016-04-22 14 views
0

私は5つのセルを表示するcollectionViewを持っています。今私が同行したいのは、たとえば、最初のセルをタップして、次のセルのimageViewがimageOneで、2番目のセルをタップした場合、imageViewはimageTwoになります。 detailViewControllerに格納します。UICollectionViewセルの画像ビューがindexpath.rowに従って選択されました

これを達成しようとしています。ここで私は、このセルをタップ私はそれ

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{ 
    if([segue.identifier isEqualToString:@"MainPush"]){ 
     NSIndexPath *indexPath = [self.collectionView.indexPathsForSelectedItems objectAtIndex:0]; 
     NSLog(@"%li", indexPath.row); 

// Getting the collectionView cell in the destination class 
     if(indexPath.row == 0){ 
     DetailCollectionViewCell *cell = [[DetailCollectionView alloc] init]; 
     cell.imageView.image = [UIImage imageNamed: @"Thailand"]; 
     } 

    } 
} 

にUiCollectionViewとUIViewのコントローラで、これまでprepareForセグエで をやったものです、それはこのように正確な別のcollectionViewに行くが、私は、画像をしたいです選択されたcollectionViewはindexpathに従って選択されます。

私はこれを行うが、何も

+0

詳細ビューコントローラに画像を渡そうとしていますか?別の言葉では、セルが選択されているときに別のビューコントローラに移動しようとしていますか? – NeilNie

答えて

0

はあなたが詳細ビューコントローラにUIimageを渡そうとしていると仮定起こりませんでした。ここに私の提案があります。まず、これを実装します。

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{ 
    CurrentImage = [imageArray objectAtIndex:indexPath.row]; //assume what you have an image object and you have an array contains images. 
    [self performSegueWithIdentifier:@"MainPush" sender:nil]; 
} 

このメソッドは、セルをクリックすると呼び出されます。内部では、コードは、mainViewController内にイメージオブジェクトがあるものとみなし、イメージを含む配列を持っています。そのすぐ下で、performSegueメソッドが呼び出されます。 は、これは重要な部分である

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{ 

    if ([[segue identifier] isEqualToString:@"MainPush"]) { 
     DetailedController *controller = (DetailedController *)[segue destinationViewController]; 
     controller.image = CurrentImage; 
    } 
} 

あなたのIDを使ってストーリーボードにSegueWayを設定することを忘れないでください。私は、あなたのviewControllerの名前がDetailedControllerだと仮定します。 1.宛先コントローラにイメージオブジェクトを作成する必要があります。 2.セルをクリックすると、currentImageオブジェクトが、ユーザがクリックしたイメージに設定され、DetailViewControllerに渡されます。

DetailController ViewDidLoadメソッドでは、UIimageView.imageをimageに設定できます。

関連する問題