简体   繁体   中英

How to find out which collection view cell is tapped inside collection view cell

I am having a table view and inside it a collection view. I want to find out which collection view cell is tapped and also indexpath.row of corresponding table view cell.(Right now what i am able to get indexpath.row of collection view cell from didSelectRowAtIndexPath() but not able to find out there in which table view cell corresponding cell is present.)

Try below code, let me know if it works for you:

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{

    UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
    CGRect frame = [collectionView convertRect:cell.frame toView:tblView];
    NSIndexPath *tblIndexPath = [tblView indexPathForRowAtPoint:frame.origin];
}

Some corrections first.

Collection view delegate method is didSelectItemAt not didSelectRowAtIndexPath(). Also since you are using collection view get the item number by using indexPath.item not indexPath.row (you can use row but collection view has no rows it has collection of items. So using item is intuitive)

Now for your query you can add cell.tag = indexPath.row in cellForRowAtIndexPath: of table view

Now inside your collection view delegate method collectionView(_:didSelectItemAt:) : you can use self.tag to know which table view cell your collection view is in.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM