当前位置: 动力学知识库 > 问答 > 编程问答 >

ios - How to fetch CollectionViewCell details on clicking button?

问题描述:

This question already has an answer here:

  • Getting row of UITableView cell on button press

    8 answers

网友答案:

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
}
网友答案:

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.
}
分享给朋友:
您可能感兴趣的文章:
随机阅读: