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

标签 ios objective-c uibutton uicollectionview uicollectionviewcell

<分区>

我都经历过

How to get the selected CollectionViewCell in the UICollectionView?

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

但是我的问题与上面两个有点不同。

我的 mybookingsCell 上有来自 nextByCollectionView 的审核按钮

- (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
{
  NearbyCell *clickedCell = (NearbyCell *)[[sender superview] superview];
  int *clickedButtonIndexPathRow = (UIButton*)sender.tag;
  // Using this you can get data from your data source arrray.
}

关于ios - 如何在单击按钮时获取 CollectionViewCell 详细信息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31671096/

上一篇:ios - Swift,应用程序在模拟器的后台运行,但不在设备上运行

下一篇:ios - 通过快速枚举从 PLIST 获取数据

相关文章:

ios - 无法在导航栏上设置为 "UIBarPositionTopAttached"

ios - 伙伴构建的替代方案

objective-c - 检测四个手指手势(Cocoa)

ios - UIButton 不调用 ImageView 中的处理函数

ios - 如何使用OCMock测试UIButton的setTitle

ios - UIButton 和 UIImage 。将按钮的图像设置为零后更改框架不会删除图像

ios - 如何在 swift 3 中按下 MKPointAnnotation 时调用函数

ios - 如何在iOS中以编程方式设置UITableView背景颜色

iphone - 转换发送方参数

objective-c - 并发绘制矩形: