【问题标题】:How to fetch CollectionViewCell details on clicking button? [duplicate]如何在单击按钮时获取 CollectionViewCell 详细信息? [复制]
【发布时间】:2015-10-18 16:49:57
【问题描述】:

我都经历过

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]; 
}

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

【问题讨论】:

  • UITableViewUICollectionView 都支持重复中提到的这种行为。
  • 删除collectionView: cellForItemAtIndexPath:中DetailObject的alloc/init。如果应用程序崩溃,您应该会收到一条错误消息。您是否检查过 [[sender superView] superview] 确实是 NearByCell 对象?
  • @iosDev82 正确阅读问题stackoverflow.com/questions/7504421/…,然后再发表评论。他在说“问题是我不知道如何知道在哪个单元格上按下了按钮。”我的不一样。不要轻易投反对票
  • @ShrikantKankatti 请通过我发布的链接查看此答案stackoverflow.com/a/16270198/1891327。我知道您的问题可以通过这种方式解决。并且不要使用superView.superView,因为它可能因单元格而异。
  • @iosDev82 我知道这是错误的,这就是我来到这里的原因,把我尝试过的代码放在这里。

标签: ios objective-c uibutton uicollectionview uicollectionviewcell


【解决方案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.
}

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-03-03
      • 1970-01-01
      • 2017-04-11
      • 1970-01-01
      • 1970-01-01
      • 2018-06-17
      • 1970-01-01
      相关资源
      最近更新 更多