0

こんにちは、UIsegmentedcontrolでUICollectionビューのセル情報にアクセスしようとしています。UICollectionビューのセル情報にアクセスできない

コレクションビューでは、4つのラベルとUIセグメント化コントロールがあります。セグメント化されたコントロールをタップすると、ラベル値を表示します。

ここは私のコードです。

- (UIView *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{ 
CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CELL" forIndexPath:indexPath]; 
cell.mySegmentedControl.tag = indexPath.row; 
selectedSegment = cell.mySegmentedControl.selectedSegmentIndex; 
[cell.mySegmentedControl addTarget:self action:@selector(segmentValueChanged:) forControlEvents:UIControlEventValueChanged]; 
cell.caseid.text=[tmpDict objectForKey:@"CaseId"]; 
caseid = [tmpDict objectForKey:@"CaseId"]; 
} 

- (void) segmentValueChanged: (UISegmentedControl *) sender { 
//NSInteger index = sender.tag; 
if(sender.selectedSegmentIndex == 0) 
{ 
NSString *localcaseid = caseid; // it shows default value may be first cell value. 
CollectionViewCell * cell = [[CollectionViewCell alloc]init]; 
NSString *localcaseid = cell.caseid.text; //it prints null value 
} 
else 
{ 
} 

上記のコードは私のためには機能しません。何か助けになるでしょう。その特定のセルに関する情報を表示したいと思います。あなたのViewControllerで

+0

をデリゲートを割り当てますsegmentValueChanged()メソッド内で、コントローラを取得しています? – Aneesh

+0

いいえ............. –

+0

基本的な問題は、UICollectionViewCellインスタンスを追跡していないことです。あなたが 'UICollectionViewCell'の新しいインスタンスを作成するとき、それはあなたが期待するものではありません。カスタムデリゲートを作成し、そのセルをパラメータとして渡すと、この問題は解決されます。さらなる助けが必要な場合は、私に知らせてください。 – dirtydanee

答えて

1
if(sender.selectedSegmentIndex == 0) { 
     NSString *localcaseid = caseid; 
     NSIndexPath *tempIndexPath = [NSIndexPath indexPathForRow:sender.tag inSection:0]; 
     CollectionViewCell *cell = (CollectionViewCell *)[CollectionView cellForItemAtIndexPath:tempIndexPath]; 
     NSString *localcaseid = cell.caseid.text; 
     NSLog(@"%@",localcaseid); 
    } 
    else { 
    } 
+0

CollectionViewCell * cell = [self.myCollectionView cellForItemAtIndexPath:tempIndexPath];私はこのようにしようとしました。互換性のないポインタ –

+0

を表示します。これは最初のセルだけに印刷されます。 –

+0

今それをチェックしてください私は答えを更新しました –

1
// CustomCell.h 

#import <UIKit/UIKit.h> 
@protocol CollectionViewCellDelegate; 

@interface CustomCollectionViewCell : UICollectionViewCell 

@property(weak, nonatomic) id<CollectionViewCellDelegate> delegate; 

@end 

// CustomCell.m 

#import "CollectionViewCell.h" 

// Create your delegate 
@protocol CollectionViewCellDelegate <NSObject> 
- (void)collectionViewCell:(CustomCollectionViewCell *)cell segmentedControlChangedValue:(UISegmentedControl *)control; 
@end 

@implementation CustomCollectionViewCell 

// Implement this delegate call on the cell, not on the viewController 
- (void) segmentValueChanged: (UISegmentedControl *) sender { 
    // Call your delegate with the cell added as parameter 
    [self.delegate collectionViewCell:self segmentedControlChangedValue:sender]; 
} 

@end 

、それはデリゲートに確認されていることを確認し、あなたのViewController宣言の隣に<CollectionViewCellDelegate>を追加し、細胞をインポートファイルヘッダー。

// ViewController.h

#import "CustomCollectionViewCell.h" 

@interface ViewController : UIViewController <CollectionViewCellDelegate> 

// ViewController.m

// VC

あなたにcellForItemに
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { 
    // create your cell 
    // assign your cell's delegate to self 
    cell.delegate = self 
} 

    // Declare the delegate method on the viewController 
    - (void)collectionViewCell:(CustomCollectionViewCell *)cell segmentedControlChangedValue:(UISegmentedControl *)control { 
     // here you now have access to the cell, where the segmented was pressed in 
     // Do what you need to and make sure you reload the data at the end of this function, when you have set up your cell 

     [self.collectionView reloadData]; 
    } 
関連する問題