present DocumentInteractionController inside tableview XIB containing a collectionView

170 views Asked by At

Im trying to load a tableview xib containing a collectionView. collectionView contains a list of files as to be downloaded and opened.

class CommentsCell: UITableViewCell, UICollectionViewDelegate, UICollectionViewDataSource, UIDocumentInteractionControllerDelegate {

var dic = UIDocumentInteractionController()
var imgCollection: [TicketAttachment] = [TicketAttachment]()
@IBOutlet weak var collectionView: UICollectionView!
@IBOutlet weak var imgProfilePic: UIImageView!
@IBOutlet weak var lblName: UILabel!
@IBOutlet weak var lblDate: UILabel!
@IBOutlet weak var txvComments: UITextView!
override func awakeFromNib() {
    super.awakeFromNib()
    dic.delegate = self
    self.collectionView.dataSource = self
    self.collectionView.delegate = self
    self.collectionView.register(UINib.init(nibName: "AttachmentViewCell", bundle: nil), forCellWithReuseIdentifier: "AttachmentViewCell")
}


func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let fileUrl = imgCollection[indexPath.row].fileUrl?.absoluteString
        let url = URL(string: Api.domain + fileUrl!)

        let destination = DownloadRequest.suggestedDownloadDestination(for: .documentDirectory)

        sharedAFManager.AFManager.download(url!, to: destination)
        .downloadProgress(closure: { _ in
            SVProgressHUD.show()
        }).response(completionHandler: { (downloadResponse) in
            SVProgressHUD.dismiss()
            self.dic.url = downloadResponse.destinationURL
            self.dic.uti = downloadResponse.destinationURL!.uti
            let rect = CGRect(x: 0, y: 0, width: 100, height: 100)
            self.dic.presentOpenInMenu(from: rect, in: self.view, animated: true)
        })
}

self.dic.presentOpenInMenu(from: rect, in: self.view, animated: true) Value of type 'CommentsCell' has no member 'view'

Tableview XIB design:

Tableview XIB design file

2

There are 2 answers

0
Sateesh Yemireddi On BEST ANSWER

Pass viewController object to UITableViewCell and replace line with

self.dic.presentOpenInMenu(from: rect, in: vc.view, animated: true)

In ViewController:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    .....
    cell.vc = self
}

In CommentsCell:

class CommentsCell: UITableViewCell, UICollectionViewDelegate, UICollectionViewDataSource, UIDocumentInteractionControllerDelegate {

    weak var vc: UIViewController!

    ........

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let fileUrl = imgCollection[indexPath.row].fileUrl?.absoluteString
        let url = URL(string: Api.domain + fileUrl!)

        let destination = DownloadRequest.suggestedDownloadDestination(for: .documentDirectory)

        sharedAFManager.AFManager.download(url!, to: destination)
            .downloadProgress(closure: { _ in
                SVProgressHUD.show()
            }).response(completionHandler: { (downloadResponse) in
                SVProgressHUD.dismiss()
                self.dic.url = downloadResponse.destinationURL
                self.dic.uti = downloadResponse.destinationURL!.uti
                let rect = CGRect(x: 0, y: 0, width: 100, height: 100)
                self.dic.presentOpenInMenu(from: rect, in: vc.view, animated: true)
            })
    }
}
0
Jatin Kathrotiya On

You can not presentOpenInMenu from UIView. You need to use UIviewContoller's instance to Present ViewController so You can simply Pass View Controller object in tableview cell or use below Extension of uiview

extension UIView {

    var parentViewController: UIViewController? {
        var parentResponder: UIResponder? = self
        while parentResponder != nil {
            parentResponder = parentResponder!.next
            if let viewController = parentResponder as? UIViewController {
                return viewController
            }
        }
        return nil
    }

}

and presentOpenInMenu like

self.parentViewController?.presentOpenInMenu(from: rect, in: self.view, animated: true)