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

时间:2015-07-28 08:29:24

标签: 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上有一个来自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]; 
}

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

2 个答案:

答案 0 :(得分:1)

我已对您的代码进行了更改

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

答案 1 :(得分:0)

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

- (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
}
相关问题