iosswiftuitableviewuidocumentinteractioncontroller

present DocumentInteractionController inside tableview XIB containing a collectionView


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


Solution

  • 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)
                })
        }
    }