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. 
} 
相關問題