0

I have gone through both

How to get the selected CollectionViewCell in the UICollectionView?

how to access from UICollectionViewCell the indexPath of the Cell in UICollectionView

But my question is little bit different from the above two.

I have review button on mybookingsCell which is from nearByCollectionView

- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath
{

   DetailObject * Obj=[[DetailObject alloc]init];

   Obj=[self.listOfObjects objectAtIndex:indexPath.row];  

  // DetailObject is NSObject class and listOfObjects getting values from API 
  // mybookingsCell.headerLabel.text=Obj.name; like this i am putting the values in cell 

    NearbyCell *mybookingsCell = (NearbyCell *)[cv dequeueReusableCellWithReuseIdentifier:@"nearby" forIndexPath:indexPath];

  [mybookingsCell.reviewBtn addTarget:self action:@selector(didTapReviewBtn:) forControlEvents:UIControlEventTouchDown];
}

-(IBAction)didTapReviewBtn:(id)sender
{

}

On clicking review button i need details which are on the cell. I tried like this

-(IBAction)didTapReviewBtn:(id)sender
{
  NearbyCell *clickedCell = (NearbyCell *)[[sender superview] superview];
  NSIndexPath *clickedButtonIndexPath = [self.nearByCollectionView indexPathForCell:clickedCell]; 
}

But my application is crashing. So help me how can i fetch cell details with corresponding click on button.

Community
  • 1
  • 1
Shrikant K
  • 1,988
  • 2
  • 23
  • 34
  • `UITableView` and `UICollectionView` both support this behavior as mentioned in the duplicate. – Shamas S Jul 28 '15 at 08:31
  • Remove the alloc/init of DetailObject in `collectionView: cellForItemAtIndexPath:`. If application is crashing, you should have an error message. Did you check that [[sender superView] superview] is indeed a `NearByCell` object? – Larme Jul 28 '15 at 08:33
  • @iosDev82 Read question properly http://stackoverflow.com/questions/7504421/getting-row-of-uitableview-cell-on-button-press before you commenting. He is saying " The problem is I don't know how to know on which cell a button is being pressed." Mine is different. Don't do downvote easily – Shrikant K Jul 28 '15 at 08:46
  • @ShrikantKankatti Please go through the link that I've posted and see this answer here http://stackoverflow.com/a/16270198/1891327. I KNOW your issue can get fixed this way. And don't use `superView.superView`, since it can vary from cell to cell. – Shamas S Jul 28 '15 at 08:48
  • @iosDev82 i know that is wrong that is the reason i came here, putting my code which i have tried. – Shrikant K Jul 28 '15 at 08:52
  • @ShrikantKankatti So the link that I've posted doesn't help you? – Shamas S Jul 28 '15 at 08:53
  • @Larme yeah i removed those lines. I have updated UIButton *btn=sender; NSLog("%@",[self.listOfObjects objectAtIndex:btn.tag];); it worked for me :) – Shrikant K Jul 28 '15 at 08:55

2 Answers2

1

I have made changes in your code

- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath
{

       DetailObject * Obj=[[DetailObject alloc]init];

       Obj=[self.listOfObjects objectAtIndex:indexPath.row];  

      // DetailObject is NSObject class and listOfObjects getting values from API 
      // mybookingsCell.headerLabel.text=Obj.name; like this i am putting the values in cell 

        NearbyCell *mybookingsCell = (NearbyCell *)[cv dequeueReusableCellWithReuseIdentifier:@"nearby" forIndexPath:indexPath];

      [mybookingsCell.reviewBtn addTarget:self action:@selector(didTapReviewBtn:) forControlEvents:UIControlEventTouchDown];
      mybookingsCell.reviewBtn.tag = indexPath.row;
}

-(IBAction)didTapReviewBtn:(id)sender
{
  NearbyCell *clickedCell = (NearbyCell *)[[sender superview] superview];
  int *clickedButtonIndexPathRow = (UIButton*)sender.tag;
  // Using this you can get data from your data source arrray.
}
Ashish P.
  • 848
  • 5
  • 12
0

Try this code to Click Button and get Get Data;

- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath
{

   DetailObject * Obj=[[DetailObject alloc]init];

   Obj=[self.listOfObjects objectAtIndex:indexPath.row];  

  // DetailObject is NSObject class and listOfObjects getting values from API 
  // mybookingsCell.headerLabel.text=Obj.name; like this i am putting the values in cell 

    NearbyCell *mybookingsCell = (NearbyCell *)[cv dequeueReusableCellWithReuseIdentifier:@"nearby" forIndexPath:indexPath];

  [mybookingsCell.reviewBtn addTarget:self action:@selector(didTapReviewBtn:) forControlEvents:UIControlEventTouchDown];
mybookingsCell.reviewBtn.tag=indexPath.row;
}

-(IBAction)didTapReviewBtn:(id)sender
{
      UIButton *btn=sender;
      NSLog("%@",[self.listOfObjects objectAtIndex:btn.tag];); // fetch you cell data here and use it
}
Dharmesh Dhorajiya
  • 3,976
  • 9
  • 30
  • 39