好吧,我的應用程式有問題。只是說我是初學者:) 我有 TableViewControler,我正在使用帶有 dequeueReusableCell 的 func 來回傳一個可以的單元格。但問題是如何使用 MFMailComposeViewController.setMessageBody 將此單元格及其值實作到正文郵件中?我正在嘗試傳遞一個在單元格中的訂單,就像在表格中一樣。該表需要在郵件正文中可見。此引數僅接受 String 并且單元格不可見(在此處的范圍內)。也許我應該將單元格轉換為 html?我該怎么做?
//我需要將這個值/單元格實作到郵件正文中//
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "OrderProductsCell", for: indexPath) as!訂單視圖單元
let rowData = orderedProducts[indexPath.row]
cell.lblNameOfOrderProduct.text = rowData.selectedProducts.name " " rowData.selectedProducts.packaging " " rowData.selectedProducts.volume " " String(rowData.stepperNumber) " x(kom)"
cell.lblFromDistributor.text = "Od"
cell.lblToStore.text = "Za"
cell.tfOrderFromDistibutor.text = String(rowData.distibutor)
cell.tfOrderToStore.text = String(rowData.store)
cell.tfOrderFromDistibutor.isUserInteractionEnabled = false
cell.tfOrderToStore.isUserInteractionEnabled = false
return cell
}
//這是 MFMailComposeViewControlles - 在同一個控制器中,如表視圖
func showMailComposer() {
guard MFMailComposeViewController.canSendMail() else {
defAlert(name: "You Can't sent mail", message: "")
return
}
let composer = MFMailComposeViewController()
composer.mailComposeDelegate = self
composer.setToRecipients(["[email protected]"])
composer.setSubject("Porudzbina")
composer.setMessageBody ("Here i need to implement cell", isHTML: false)
present(composer,animated: true)
}
uj5u.com熱心網友回復:
我不確定我是否完全理解你的最終目標,但讓我分享一下我認為你的意思。
我相信您希望表格視圖中的所有文本都在 MailView 中顯示為文本。
我知道你們所有的產品資料都在里面orderedProducts——如果我錯了,請糾正我?
所以在你的showMailComposer函式中,你可以試試這個 - 我已經為我添加到你的函式的新代碼添加了注釋:
func showMailComposer() {
guard MFMailComposeViewController.canSendMail() else {
defAlert(name: "You Can't sent mail", message: "")
return
}
// Start empty string
var stringBuilder = ""
// Loop through all your products
for orderedProduct in orderedProducts
{
// Append string builder with products
// Add all product properties you want
stringBuilder = "\(orderedProduct.name), \(orderedProduct.packaging) \n"
}
let composer = MFMailComposeViewController()
composer.mailComposeDelegate = self
composer.setToRecipients(["[email protected]"])
composer.setSubject("Porudzbina")
// Add string builder as text
composer.setMessageBody (stringBuilder, isHTML: false)
present(composer,animated: true)
}
運行這個結果并檢查它是否接近你想要的,它會給出這樣的輸出:

更新
If you want to format the text a bit better in the MFMailComposeViewController you can use HTML and you need to know some basics of HTML like table as you suggested or other techniques like div.
Here is example of your function using table to organize the output:
func showMailComposer() {
guard MFMailComposeViewController.canSendMail() else {
defAlert(name: "You Can't sent mail", message: "")
return
}
// Start htmlStringBuilder with opening table tag and 2 headers / columns
// named Name and Packaging
var htmlStringBuilder = "<table border = 1><tr><th style=\"padding:10px\">Name</th><th style=\"padding:10px\">Packaging</th></tr>"
// Loop through all your products
for orderedProduct in orderedProducts
{
// Start a new table row
htmlStringBuilder = "<tr>"
// Add name table data for this row in first column
htmlStringBuilder = "<td>\(orderedProduct.name)</td>"
// Add packaging table data for this row in second column
htmlStringBuilder = "<td>\(orderedProduct.name)</td>"
// End the current row
htmlStringBuilder = "<tr>"
}
// End html tag
htmlStringBuilder = "</table>"
let composer = MFMailComposeViewController()
composer.mailComposeDelegate = self
composer.setToRecipients(["[email protected]"])
composer.setSubject("Porudzbina")
// Add htmlStringBuilder builder as html
composer.setMessageBody (htmlStringBuilder, isHTML: true)
present(composer,animated: true)
}
The generated HTML from above code is actually like this
<table border="1">
<tr>
<th style="padding:10px">Name</th>
<th style="padding:10px">Packaging</th>
</tr>
<tr>
<td>Test</td>
<td>Test</td>
</tr>
<tr></tr>
<tr>
<td>Test 2</td>
<td>Test 2</td>
</tr>
<tr></tr>
</table>
Final result gives you data organized in a table.

This is just basic HTML and you can do more styling and change how the output looks as you wish.
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/422257.html
標籤:
上一篇:如何使用自定義按鈕在表格視圖單元格中附加和洗掉選定的行單元格?
下一篇:allowMultipleSelectionDuringEditing不適用于自定義UITableviewCell
