在包含 collectionView 的表视图 XIB 中显示 DocumentInteractionController

present DocumentInteractionController inside tableview XIB containing a collectionView

我正在尝试加载包含 collectionViewtableview xibcollectionView 包含要下载和打开的文件列表。

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 设计:

viewController 对象传递给 UITableViewCell 并将行替换为

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

在ViewController中:

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

    .....
    cell.vc = self
}

在评论单元格中:

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

您无法从 UIView 显示 OpenInMenu。您需要使用 UIviewContoller 的实例来呈现 ViewController 因此您可以简单地在 tableview 单元格中传递 View Controller 对象或使用下面的 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
    }

}

并像

一样呈现 OpenInMenu
self.parentViewController?.presentOpenInMenu(from: rect, in: self.view, animated: true)