Use the button on the UICollectionViewCell to display data from the array

I have an array of NSStrings , one UILabel and a UICollectionView .

My question is:

I want the array count to determine the number of UICollectionViewCell .

Each UICollectionViewCell contains a button. After clicking this button, I want this button to UICollectionViewCell data in the array corresponding to the UICollectionViewCell number that will be displayed on the label.

For example, if the user clicks on the 13 UICollectionViewCell button, then the 13th NSString in the array becomes the text UILabel .

What I've done:

I created my own subclass of UICollectionViewCell for the nib file, which I use for all UICollectionViewCell s, and connected the button to the .h file as IBAction . I also imported MainViewController.h , which contains an array property that stores NSString s.

When I edit the code in the UICollectionViewCell action, I cannot access the property of the array. Button works - I put NSLog in the IBAction method, which works.

I searched for dozens of other answers to SO, but no one answered my specific question. I can update this with my code samples if necessary.

+7
source share
3 answers

I created my own subclass of UICollectionViewCell for the nib file that I use for all UICollectionViewCells, and in the .h file as an IBAction.

If you connect IBAction to a subclass of the ViewCell collection, you will need to create a delegate to make the touch event available in the viewController where you are displaying the data.

One simple setup is to add the collectionViewCell button, connect it to IBOutlet in the cell. But not IBAction. In cellForRowAtIndexPath: add an eventHandler for the button in this viewController containing the collectionView.

 - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { //Dequeue your cell [cell.button addTarget:self action:@selector(collectionViewCellButtonPressed:) forControlEvents:UIControlEventTouchUpInside]; return cell; } - (IBAction)collectionViewCellButtonPressed:(UIButton *)button{ //Acccess the cell UICollectionViewCell *cell = button.superView.superView; NSIndexPath *indexPath = [self.collectionView indexPathForCell:cell]; NSString *title = self.strings[indexPath.row]; self.someLabel.text = title; } 
+15
source

Please try like this.

In YourCollectionViewCell.h

Create an IBOutlet not IBAction call button for the UIButton that you added to xib. Remember that you must connect the outlet to the cell object not the owner of the file in xib.

MainViewController.m

  - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { cell.button.tag = indexPath.row; [cell.button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside]; return cell; } -(void)buttonPressed:(UIButton*)sender { NSLog(@"%d : %@",sender.tag,[array objectAtIndex:sender.tag]); self.textLabel.text = [array objectAtIndex:sender.tag]; } 

Edit - handle multiple sections

  -(void)buttonPressed:(UIButton*)sender { NSIndexPath *indexPath = [self.collectionView indexPathForCell: (UICollectionViewCell *)sender.superview.superview]; NSLog(@"Section : %d Row: %d",indexPath.section,indexPath.row); if (0 == indexPath.section) { self.textLabel.text = [firstArray objectAtIndex:indexPath.row]; } else if(1 == indexPath.section) { self.textLabel.text = [secondArray objectAtIndex:indexPath.row]; } } 
+1
source

When I edit the code in the UICollectionViewCell action, I cannot access the property of the array.

This is because you associated the button action with the β€œwrong” object. It must be connected to the MainViewController (or anyone who has access to the array property).

You will have several tasks:

  • Receive a message about the action of the button.

  • Access to an array (model for data).

  • Drop the switch, which indicates which cell should now have its own label.

  • Tell us about the presentation of the reloadData collection, thereby updating the cells.

All these tasks most conveniently relate to one object. I assume this is a MainViewController (and therefore I assume that MainViewController is the delegate / data source of the collection view).

0
source

All Articles