1

I am a little new to iOS and I am working with a UICollectionView. My UICollectionView cells are populated with values from my array from my restful server. So I pull those down and assign the fields, ID, Name and text. When I tap a specific cell. I want to send the ID Of that cell (The ID which we just pulled down) and send it as a parameter to my php. So far I am populating my collectionview with the IDs, but my problem occurs when I tap the cell, I am unsure how to get the ID.In the didSelectCellAtIndex method I am trying to get the cell id using

UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
//dictionary[indexPath.row].;
cell.

However I am not sure how to refer to the property I want. Since in

cellForItemAtIndexPath

I defined the label I want as this

UILabel *myid=(UILabel *)[cell viewWithTag:1];

Any help to assigning the ID in the label to a variable to send to my php will be appreciated.

Bannings
  • 9,960
  • 6
  • 41
  • 52
Step Add
  • 11
  • 4

2 Answers2

1

I'd suggest you to create a custom cell (subclass of UICollectionViewCell) with properties for each subview thats needs to be configured (IBOutlet if you using xib/storyboard). So that they can be accessed. Like so:

@interface MyCollectionViewCell : UICollectionViewCell

@property (nonatomic, weak) IBOutlet UILabel *IDLabel;

@end

You can access it directly:

MyCollectionViewCell *cell = (MyCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
cell.IDLabel

Or like this:

UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
UILabel *myid=(UILabel *)[cell viewWithTag:1];
NSString *id = myid.text;

I think you can also access the ID from your dataSource(e.g. IDs) through the indexPath.

NSString *id = IDs[indexPath.row];
Bannings
  • 9,960
  • 6
  • 41
  • 52
  • Hi The second method worked, Id actually thought of this but, I was of the assumption this would create a new label with tag 103. not reference the one already existing. However while experimenting i discovered if i wrote dictionary[indexPath.row]. ( . connotation ) it would load a list of properties and objects including the id I was trying to access but I was unsure how to assign it to a string or could it even be done from there?. (PS. your third method seemed similar to it but that doesnt work) – Step Add Jul 07 '15 at 05:10
  • Why doesn't work the third method? Are you using `dictionary[indexPath.row]`? It seems like a dictionary not a array. – Bannings Jul 07 '15 at 05:17
  • Like I said I used the second method and not sure why the third doesnt work, was hoping you'd shed some light – Step Add Jul 07 '15 at 10:28
  • What is IDs? the the equivalent of the label i called myid? (refer to my code) – Step Add Jul 07 '15 at 13:55
  • No, It should be your data source. How do you give `myid.text` a value? – Bannings Jul 07 '15 at 14:12
0

The Best way to Create UICollectionView is from the below link

Creating a UICollectionView programmatically

In this code their is a method cellForItemAtIndexPath in that you create subview inside it like below

if you want to use UILabel as Subview then follow this Code.

UILabel *lblName = (UILabel *)[cell viewWithTag:1];

if(lblName == nill)
{

//initalize the label.
    lblName =[[UIlabel alloc]init];   
    lblName.tag=1;
}

else

{

//assign value for that label

}

you can create by this way all the subview in CollectionViewCell. Just do not forget to add tag in this else it will alway create new labels for the cell.

Community
  • 1
  • 1
Gaurav Parmar
  • 311
  • 1
  • 10