Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Force touch on UICollectionView inside of UITableViewCell

I have a viewController with a Tableview, multiple TableViewCells and in each TableViewCell, a UICollectionView with multiple UICollectionViewItems. Each collectionView item has a label and image view. I'm trying to get 3d touch to work so that the user and peek and pop by force touching on areas of the tableCell that don't contain the collection view, to preview and pop into one view controller and then be able to do the same thing with one of the images in the collectionView but preview and pop into a different view controller. I have the first scenario working fine, the tableCell remains sharp on the screen when starting to force touch and "peek". I'm stuck on getting this to work in the collection view, no matter what I do only an image view frame remains sharp on the first tableview row regardless of which row i'm actually pressing. Code below:

    func previewingContext(_ previewingContext: UIViewControllerPreviewing, viewControllerForLocation location: CGPoint) -> UIViewController? {

    //get the tableviewCell
    if let tableCellPath = tableView.indexPathForRow(at: location) {
        print("tableCellPath=", tableCellPath)
        if let tableCell = tableView.cellForRow(at: tableCellPath) as? VenueMainTableViewCell {
            //user tapped on a beer in the collectionview
            if let collectionView = tableCell.collectionView {
                let collectionPoint = collectionView.convert(location, from: tableView)
                if let cvPath = collectionView.indexPathForItem(at: collectionPoint) {
                    let collectionViewCell = collectionView.cellForItem(at: cvPath) as? VenueMainCollectionViewCell

                    let cvFrame = collectionViewCell?.itemLabelImageView.frame

                    let bc = storyboard?.instantiateViewController(withIdentifier: "itemDetail") as! ItemDetailViewController
                    let ven = UTcheckin.sharedInstance.Venues[collectionView.tag]
                    let selectedItem = ven.ItemsAtVenue[(collectionViewCell?.tag)!]
                    bc.item = selectedItem

                    previewingContext.sourceRect = cvFrame!

                    return bc
                }
            }
        }
        if let tablePath = tableView.indexPathForRow(at: location) {
            //user tapping on a venue, this works
            previewingContext.sourceRect = tableView.rectForRow(at: tablePath)
            let vc = storyboard?.instantiateViewController(withIdentifier: "venueDetail") as! VenueDetailViewController
            vc.venue = UTcheckin.sharedInstance.Venues[tablePath.row]

            return vc
        }
        return nil
    }
    return nil
}

It seems like I need to get the rect of the collection view item image view but how can I access this since it is in the table cell? Thanks in advance for any pointers.

like image 796
HH887 Avatar asked Jan 06 '17 04:01

HH887


1 Answers

I think the solution for this is the same as for UITableView. You have to register each cell for previewing using registerForPreviewingWithDelegate method. You should register it in cellForRow method.

This should be very helpful for you. Especially The Solution Paragraph:


      How to Peek & Pop A Specific View Inside a UITableViewCell

like image 61
kamwysoc Avatar answered Nov 15 '22 07:11

kamwysoc