0

我已经通过两个如何在单击按钮上获取CollectionViewCell详细信息?

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

但是我的应用程序崩溃了。所以帮助我如何通过相应的点击按钮来获取细胞的细节。

+0

所做的更改'UITableView'和'UICollectionView'都支持在重复提到的这种行为。 –

+0

删除'collectionView:cellForItemAtIndexPath:'中的DetailObject的alloc/init。如果应用程序崩溃,则应该有错误消息。你是否检查过[[sender superView] superview]确实是'NearByCell'对象? – Larme

+0

@ iosDev82正确阅读问题ht​​tp://stackoverflow.com/questions/7504421/getting-row-of-uitableview-cell-on-button-press评论之前。他说:“问题是我不知道如何知道在哪个单元格上按了一个按钮。”我的是不同的。不要轻易downvote –

回答

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