Muthu Raj Muthu Raj - 11 months ago 60
iOS Question

Unable to access UICollection view cell infromation

Hi I am trying to access UICollection view cell information through UIsegmentedcontrol.

In a collection view I have four labels and UIsegmented control.When I tap the segmented control, I want to display labels values.

here is my code.

- (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

The above code not works for me.any help will be appreciated.I want to display information for that particular cell.

Answer Source
    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;
    else {