2016-04-22 62 views
0

我有一個顯示5個單元的collectionView。現在我想要實現的是,例如,如果我點擊第一個單元格,下一個單元格的imageView是imageOne,如果我點擊第二個單元格,imageView將會是imageTwo。在detailViewController中。根據indexpath.row選擇更改UICollectionView cell imageview

我想完成這個。這裏是我一直就這麼遠 做過prepareFor Segue公司在UIView的控制器與UiCollectionView

- (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"]; 
     } 

    } 
} 

我我點擊這個細胞就進入到另一個的CollectionView確切這樣的,但我想在圖像根據所選的indexpath.row選擇不同的collectionView。

我這樣做,但什麼都沒有發生

+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方法被調用。 記得設置你的故事板SegueWay與您的ID

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

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

這是一個重要的組成部分。我假設你的viewController的名字是DetailedController。 1.您必須在目標控制器中創建一個圖像對象。 2.單擊單元格時,它會將currentImage對象設置爲用戶剛剛單擊的圖像並將其傳遞給DetailViewController。

在DetailController ViewDidLoad方法中,可以將UIimageView.image設置爲圖像。

相關問題