Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use a button on a UICollectionViewCell to display data from an array

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

My Question:

I want the array's count to determine how many UICollectionViewCell's there are.

Each UICollectionViewCell contains a button. Upon click, I want this button to cause the data in the array that corresponds to the UICollectionViewCell's number to be displayed in the label.

For example, if the user clicks on the 13th UICollectionViewCell's button, then the 13th NSString in the array would become the UILabel's text.

What I have done:

I have made my own subclass of UICollectionViewCell for the nib file that I use for all of the UICollectionViewCells, & connected the button to the .h file as a IBAction. I have also imported the MainViewController.h, which is the one that contains the array property that stores the NSStrings.

When I edit the code in the UICollectionViewCell's action, I cannot access the array property. The button does work - I placed an NSLog in the IBAction's method, which does work.

I have searched through tens of other answers on SO, but none answer my specific question. I can update this with samples of my code if requested.

like image 772
tagabek Avatar asked May 09 '13 02:05

tagabek


2 Answers

I have made my own subclass of UICollectionViewCell for the nib file that I use for all of the UICollectionViewCells, and connected the button to the .h file as a IBAction.

If you connect the IBAction to the subclass of collectionViewCell you would need to create a delegate to make the touch event available in the viewController where you are displaying the data.

One easy tweak is to add the button the collectionViewCell, connect it's IBOutlet to the cell. But not IBAction. In the cellForRowAtIndexPath: add an eventHandler for button in that viewController containing 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;

}
like image 76
Anupdas Avatar answered Nov 16 '22 01:11

Anupdas


Please try like this..

In YourCollectionViewCell.h

Create an IBOutlet not IBAction called button for the UIButton that you added to the xib. Remember you should connect the outlet to the cell object not to the file owner in the 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];
}

}
like image 37
Anil Varghese Avatar answered Nov 16 '22 03:11

Anil Varghese