2016-05-25 6 views
1

私の次のビューコントローラへのセグエを実行するための私の現在のコードは次のとおりです。取得UICollectionViewCellラベルのテキスト

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

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

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
    CustomCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath]; 
    [[cell IconImage]setImage:[UIImage imageNamed:[arrayOfImages objectAtIndex:indexPath.item]]]; 
    [[cell IconLabel]setText:[arrayOfDescriptions objectAtIndex:indexPath.item]]; 
    return cell; 
} 

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
    [self performSegueWithIdentifier:@"GroupsHomeSegue" sender:self]; 
} 

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

    if ([segue.identifier isEqualToString:@"GroupsHomeSegue"]) 
    { 


     //IconImage is the name of the image in GroupsViewController i want to pass through. Based upon what each cell is set at anyways. 

     //logoImage is the name of the image i want to set in GroupsHomeViewController. 

     //so basically i want to be able to get IconImage and set it as logoImage in a different view controller 

    } 
} 

私が午前問題は、私が選択した各から私のテキストの値を取得行う方法です私はそれを別のラベルとして詳細なビューコントローラに投稿することができます。

Segueの準備中のコメント行は、私が達成しようとしているものを正確に表しています。私は単なるUICollectionViewCellの値を取得したいと思っています。

これは、以前のビューコントローラの投稿でデータを渡すのと似ているかもしれませんが、これは私が見つけたものとは異なります。テキスト値は定数です。すなわち、label.textの値は1つのものに設定され、配列からは来ません。

個別に選択したセルのラベル値を見つけて詳細なビューコントローラに渡す方法を知りたいだけです。

I Have updated the code following examples giving and still no result as the picture does not show.

+0

をコード - 使用してみてください。 –

答えて

1

セグエ中に次のビューコントローラに情報を渡すために、あなたはprepareForSegue:sender:に渡されたパラメータにseguedestinationViewControllerプロパティを使用します。もちろん、値を設定できるようにするには、その宛先ビューコントローラでプロパティを設定する必要があります。

ユーザーが選択した情報を判断するには、いくつかの選択肢があります。ビューコントローラにプロパティを作成して、ユーザーが選択した内容を保存し、collectionView:didSelectItemAtIndexPath:のパラメータでcollectionView:didSelectItemAtIndexPath:の値を入力するか、UICollectionViewメソッドindexPathsForSelectedItemsを使用して、prepareForSegue:sender:の間に選択した項目のインデックスパスを取得できます。私は後者をする傾向がある。

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 
    if ([segue.identifier isEqualToString:@"GroupsHomeSegue"]) 
    { 
     NSIndexPath* indexPath = [[someCollectionView indexPathsForSelectedItems] first]; 
     if(indexPath != nil) 
     { 
      NSString* selectedDescription = arrayOfDescriptions[indexPath.item]; 
      NSString* selectedImageName = arrayOfImages[indexPath.item]; 

      // Get the destination view controller (the one that will be shown) from the segue and cast it to the appropriate type. Assuming this should be GroupsHomeViewController, but I'm not entirely sure that's correct since I can't see all your code 
      GroupsHomeViewController* groupsHomeViewController = segue.destinationViewController; 
      // Set the appropriate properties (Again, I'm guessing here since I can't see your code) 
      groupsHomeViewController.logoImage = [UIImage imageNamed: selectedImageName]; 
     } 
    } 
} 
+0

私のuicollectionviewcell内で自分のラベルの個々の値を取得するにはどうすればよいですか?私が使うことができる短いコードのいくつかのフォームがありますか、またはNSLogを使用して、どのセルをクリックしているかを取得する必要がありますか?正確なセルの値を見つけるためにそこから行ってください –

+0

あなたが提案している方法で 'NSLog()'をどのように使うことができるか分かりません。選択したセルの 'indexPath'を持っているコレクションビューからセルを取得し、セルのラベル(およびその他の)プロパティを調べることができます。あなたが値の配列からラベルを取り込むことを考えれば、選択したセルの 'indexPath'を使って直接配列から値を取得することもできます。私はあなたの参照のための少しのコードを追加しました。 –

+0

申し訳ありませんが、あなたがObjective-Cであることに気付いたので、サンプルコードを更新します。 –

0

"didSelectRowAtIndexPath"では、indexPathを "送信者"として渡します(下記のコードを参照してください)。また、 "performSegueWithIdentifier"では、選択したセルのindexPathを取得します。

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { 

     [self performSegueWithIdentifier:@"GroupsHomeSegue" sender:indexPath]; 
    } 

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

      if ([segue.identifier isEqualToString:@"GroupsHomeSegue"]) 
      { 
       NSIndexPath *indexPath = (NSIndexPath*)sender; 
       NSString* iconImage = arrayOfImages[indexPath.row]; 

      } 
     } 
0

これはあなたが識別子でセグエを実行行うと、あなたはまた、送信者にとセグエの準備にindexPathを送るindexPathとして送信者を型キャストし、値を取得することができます

- (void)collectionView:(UICollectionView *)collectionView  didSelectItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
[self performSegueWithIdentifier:@"GroupsHomeSegue" sender:indexPath]; 
} 

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

if ([segue.identifier isEqualToString:@"GroupsHomeSegue"]) 
{ 

    UICollectionViewCell *cell = [self.collectionView cellForItemAtindexPath:sender]; 
    NSString *text = cell.IconLabel.text; 
    UIImage *image = cell.IconImage.image; 
    GroupsHomeViewController* ghVC = segue.destinationViewController; 
    groupsHomeViewController.logoImage = image; 


} 
} 
関連する問題