2

我试图加载一个tableview xib包含一个collectionView. collectionView包含要下载和打开的文件列表。

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(来自:rect,in:self.view,动画:true)“CommentsCell”类型的值没有成员“view”

Tableview XIB 设计:

Tableview XIB 设计文件

4

2 回答 2

1

您不能从 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
    }

}

和presentOpenInMenu之类的

self.parentViewController?.presentOpenInMenu(from: rect, in: self.view, animated: true)
于 2018-11-12T07:44:09.613 回答
1

将对象传递viewControllerUITableViewCell并替换为

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

在视图控制器中:

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)
            })
    }
}
于 2018-11-12T07:37:17.047 回答