我目前正在使用iOS应用程序,该应用程序填充了持久性核心数据存储,并希望允许用户通过从UITableviewcells中选择项目来通过电子邮件发送数据。我的重用单元格已从核心数据正确填充,并为viewcontroller加载良好。如何将uitableview中的选定项目放入电子邮件中?
tl:dr我将NSMutableString声明为mailString,并尝试使用按钮isSelected追加到mailString上,并且它在我的表视图和重用单元格中无法正常工作。
我的电子邮件构造函数可以正常工作并发送正常,但是电子邮件中根本没有填充单元格数据。这是其中的一些课程(我将其剪掉以使其更易于阅读)
//some code removed to make it easier to read
class SavedDrivesViewController: UIViewController {
let mailString = NSMutableString()
/*----snip----*/
//in an Extension to SavedDrivesViewController I have:
func tableView(_ tableView: UITableView, cellForRowAt indexPath:
IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier:
/*----snip----*/
//How can I do this cleaner in Swift 4 or 5?
if let buttonCheck = cell.contentView.viewWithTag(4) as? UIButton {
buttonCheck.addTarget(self, action: #selector(checkBoxClicked(_
:)), for: .touchUpInside)
}
return cell
}
//I hate nested functions and they are crap how can I redo this?
@objc func checkBoxClicked(_ sender: UIButton){
sender.isSelected = !sender.isSelected
if sender.isSelected == true {func setSelected(_ tableView:
UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = tableView.cellForRow(at: indexPath) as!
DriveTableViewCell
mailString.append("\n\(String(describing:
cell.distanceLabel?.text)),\(String(describing:
cell.timeLabel?.text)), \
(String(describing: cell.notesLabel?.text))!)")
}
}
}
func configureMailController() -> MFMailComposeViewController {
let mailComposerVC = MFMailComposeViewController()
mailComposerVC.mailComposeDelegate = self
mailComposerVC.setToRecipients(["my_email_addy"])
mailComposerVC.setSubject("Some logs")
mailComposerVC.setMessageBody(mailString as String, isHTML: false)
return mailComposerVC
}