如何在单击按钮时获取 CollectionViewCell 详细信息?

How to fetch CollectionViewCell details on clicking button?

我都经历过

How to get the selected CollectionViewCell in the UICollectionView?

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

不过我的问题和上面两个有点不同

我在 mybookingsCell 上有来自 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
{

}

单击“审阅”按钮后,我需要单元格上的详细信息。 我试过这样

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

但是我的应用程序崩溃了。所以请帮助我如何通过相应的单击按钮获取单元格详细信息。

尝试使用此代码点击按钮并获取数据;

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

我已经修改了你的代码

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