swiftuitableviewwkwebview3dtouch

Enabling 3D Touch preview for TableView


I've got TableViewController that segues to UIViewController where WKWebView is located. Every TableView cell points to particular local html file, which is then loaded in WebView.

I can simply check the Peek & Pop - Preview & Commit Segues in Attributes Inspector for that segue, but the preview will show always the same html file, rather than different one for each table cell selected.

What is the proper way to implement 3D Touch, or how to somehow prepare the html content for preview in the first TableViewController?


.xcodeproj on my github. More info on demand.

Thanks for help!


Solution

  • There are no selected cells when peeking, therefore you cannot just ask the table about the selected cell. Instead, you have to handle the sender parameter, which is the UITableViewCell:

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "transporter" {
            guard
                let cell = sender as? UITableViewCell,
                let path = table.indexPath(for: cell)
            else {
                return
            }
    
            let selected = filtering() ? filter[path.section].rows[path.row] : list[path.section].rows[path.row]
            let controller = (segue.destination as! Detail)
            controller.result = selected
        }
    }