0
votes

Well I have a problem with my app. Just to say I m a beginner :) I have TableViewControler and I am using func with dequeueReusableCell to return a cell I that is ok. But the problem is how to implement this cell(s) with their values into a body mail using MFMailComposeViewController.setMessageBody? I m trying to pass an order which is in cells just like it is - in a table. That table a need to be visible in mail body. This parameter only takes String and cell is not visible (in scope here). Maybe should I transform cell into a html? And how should I do that?

//This values/cells I need to implement into a mail body//

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "OrderProductsCell", for: indexPath) as! OrderViewCell

    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
    
}

//This is MFMailComposeViewControlles - in the same Controller like Table View

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)
}
1
Do you just need the data that is in the tableView as text into the Mail view or you want the image of the TableView in your mail ?Shawn Frank
Maybe you can add some code of tableview data methods such as numberOfRowsInSection and cellForRowAt indexPath which will be helpful to us to understand your issue.Shawn Frank
@ShawnFrank I don't have images in cells only labels and text fields. So just data. I tried using that by index Path can't be found in scope inside MFMailComposeViewControler. I don't know is that enough you to understand, its a little confusing question...Suncica Miladinovic
I think you should paste the code of what you tried which is crashing and this will be useful for anyone seeing this question and myself to debug your issue.Shawn Frank
@ShawnFrank I edited the question with code. Nothing is crashing because I don't know how to do it :)Suncica Miladinovic

1 Answers

0
votes

I am not sure I understand your end goal completely, but let me share what I think you mean.

I believe you want all the texts from your table view to show inside the MailView as text.

And I understand all your products data is inside orderedProducts - correct me if I am wrong ?

So inside your showMailComposer function, you can maybe try this - I have added comments to new code I have added to your function:

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

Run this result and check if it is close to what you want.