gpt4 book ai didi

ios - 如何使用 UIDocumentPicker 将 pdf 文件导入 TableView 单元格

转载 作者:行者123 更新时间:2023-11-30 11:35:04 26 4
gpt4 key购买 nike

我真的很困惑如何导入 pdf 文件并将其存储在 TableView 单元格中。我正在使用 UIDocumentPicker 导入 pdf 文件,但我无法弄清楚如何将 pdf 文件数据存储到 TableView 单元格中。我的问题是,我创建了一个名为 insertNewFileTitle() 的函数,每次单击 addButtonTapped 函数时,该函数都会附加一个新的 TableView 单元格。我想确保 pdf 数据仍然位于我导入 pdf 文件的正确表格 View 单元格中。

我想做的是让表格 View 单元格存储导入的 pdf 文件,以便稍后在选择表格单元格时我可以将 pdf 文件作为邮件编辑器的附件发送。如有任何见解,我们将不胜感激,谢谢。

var pdfFiles: [String] = []
var myPDFsArray : Array<MyPDFs> = []
var selectedRow : NSInteger? // optional NSInteger

@available(iOS 8.0, *)
public func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentAt url: URL) {

let myURL = url as URL
print("import result : \(myURL)")

do {
let data = try Data(contentsOf: myURL)
print("data=\(data)")

}
catch {/* error handling here */}

let pathString = myURL.path // String path for PDF
let PDFName = myURL.lastPathComponent // string for PDF Name

if controller.documentPickerMode == UIDocumentPickerMode.import {
DispatchQueue.main.async(){
self.myPDFsArray.append(MyPDFs(fileTitle: pathString, fileName: PDFName))

}
}
print (pathString)
print (PDFName)
}

@available(iOS 8.0, *)
public func documentMenu(_ documentMenu:UIDocumentMenuViewController, didPickDocumentPicker documentPicker: UIDocumentPickerViewController) {
documentPicker.delegate = self
present(documentPicker, animated: true, completion: nil)
//self.present(documentPicker, animated: true, completion: nil)
}
func documentPickerWasCancelled(_ controller: UIDocumentPickerViewController) {
print("we cancelled")

dismiss(animated: true, completion: nil)
}

func documentMenuWasCancelled(documentMenu: UIDocumentMenuViewController) {
print("menu got cancelled")
documentMenu.dismiss(animated: true, completion: nil)

}

@IBAction func addButtonTapped(_ sender: Any){
let importMenu = UIDocumentMenuViewController(documentTypes: [String(kUTTypePDF)], in: .import)
importMenu.delegate = self
importMenu.modalPresentationStyle = .formSheet
self.present(importMenu, animated: true, completion: nil)
//importMenu.addOption(withTitle: "Create New Document", image: nil, order: .first, handler: {print("New Doc Requested")})
insertNewFileTitle()
}

func insertNewFileTitle(){
if addFileTextField.text!.isEmpty {
print("Add Video Text Field is empty")
}
pdfFiles.append(addFileTextField.text!)

let indexPath = IndexPath(row: pdfFiles.count - 1, section: 0)
tableView.beginUpdates()
tableView.insertRows(at: [indexPath], with: .automatic) //can customize
tableView.reloadData()//reload table
tableView.endUpdates()
addFileTextField.text = ""
view.endEditing(true)
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return pdfFiles.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let fileTitleCell = pdfFiles[indexPath.row]
let cell = tableView.dequeueReusableCell(withIdentifier: "FileCell") as! FileCell
cell.fileTitle.text = fileTitleCell

if myPDFsArray.contains(MyPDFs(fileTitle: "fileTitle", fileName: "fileName")){

cell.accessoryType = .checkmark
}

else{
cell.accessoryType = .none
}
return cell
}


func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
selectedRow = indexPath.row // indexPath.row is the value of the row you tap on, starting from first 0
tableView.deselectRow(at: indexPath, animated: true)
if let cell = tableView.cellForRow(at: indexPath as IndexPath) {
//selectedRow = indexPath.row // indexPath.row is the value of the row you tap on, starting from first 0
//if myPDFsArray.contains(MyPDFs(fileTitle: "fileTitle", fileName: "fileName")){
if cell.accessoryType == .checkmark{
cell.accessoryType = .none
//myPDFsArray.remove(at: MyPDFs.index(ofAccessibilityElement: NSObject.self))

//print(MyPDFs.init(fileTitle: "fileTitle", fileName: "fileName"))
print("")
print(myPDFsArray[indexPath.row].fileTitle!)
print("")
print(myPDFsArray[indexPath.row].fileName!)
}
else{
cell.accessoryType = .checkmark
selectedRow = indexPath.row
myPDFsArray.append(MyPDFs(fileTitle: "fileTitle", fileName: "fileName"))
//print(MyPDFs.init(fileTitle: "fileTitle", fileName: "fileName"))
print("")
print(myPDFsArray[selectedRow!].fileTitle!)
print("")
print(myPDFsArray[selectedRow!].fileName!)
}
//}
}
}

下一部分是我发送电子邮件的按钮操作

@IBAction func sendEmail(_sender: Any) {     
let subject = "subject"
let messageBody = "pdf file names"
lblValidationMessage.isHidden = true

let allPaths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
let documentsDirectory = allPaths.first!
let myPath = "/(MyPDFs[selectedRow].fileName)" //is it a forward slash or backslash?
let pathForFile = documentsDirectory.appending(myPath)
print("")
print(pathForFile)
print("")

guard let email = emailNew.text, emailNew.text?.characters.count != 0 else {
lblValidationMessage.isHidden = false
lblValidationMessage.text = "Please enter your email"
return
}

if isValidEmail(emailNew: email) == false {
lblValidationMessage.isHidden = false
lblValidationMessage.text = "Please enter a valid email address"
return
}

if( MFMailComposeViewController.canSendMail() ) {
print("Can send email.")

let mailComposer = MFMailComposeViewController()
mailComposer.mailComposeDelegate = self

//Set the subject and message of the email
mailComposer.setToRecipients([email])
mailComposer.setSubject(subject)
mailComposer.setMessageBody(messageBody, isHTML: false)


// Add attachment
if let fileData = NSData(contentsOfFile: pathForFile) {
print("file data loaded")
mailComposer.addAttachmentData(fileData as Data, mimeType: "application/pdf", fileName: "fileName")
}

//this will compose and present mail to user"
self.present(mailComposer, animated: true, completion: nil)
}
else {
self.showSendMailErrorAlert()
}
}

最佳答案

我不确定我是否正确理解了这一点。为什么需要将 PDF 存储在 UITableView 单元格内?

为什么不直接创建一个这样的模型:

class MyPDFs : NSObject
{
var fileTitle : String?
var fileName : String?

override init(){}

convenience init(fileTitle : String , fileName : String)
{
self.init()

self.fileTitle = fileTitle
self.fileName = fileName
}
}

每次点击 addButton 时,都会创建一个新对象,将其附加到 MyPDFs 类型的数组中,然后重新加载表格。

var myPDFsArray : Array<MyPDFs> = []
myPDFsArray.append(MyPDFsObject)
myUITableView.reloadData()

这样,当您使用indexPath.row选择UITableView单元格时,您就可以获取PDF属性,并且可以获得要附加到MailComposer的名称和文件路径。

关于ios - 如何使用 UIDocumentPicker 将 pdf 文件导入 TableView 单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49850460/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com