我正在构建一个具有三个tableViews的应用程序:一个用于俱乐部,它导致另一个成员,并导致交易。前两个tableViews及其“ add”控制器正常工作,而第三个则无效。据我所知,add控制器可以正常工作,因为我知道该事务已保存到Core Data。但是该事务未显示在tableView中,我也不知道如何解决此问题,因为这是我的第一个应用程序。
交易Vc代码
import UIKit
import MessageUI
import CoreData
class TransactionViewController: UIViewController, MFMailComposeViewControllerDelegate, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var tableViewTransaction: UITableView!
let dateFromatter = DateFormatter()
var member: Member?
override func viewDidLoad() {
super.viewDidLoad()
dateFromatter.timeStyle = .long
dateFromatter.dateStyle = .long
}
override func viewWillAppear(_ animated: Bool) {
self.tableViewTransaction.reloadData()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func addNewTransaction(_ sender: Any) {
performSegue(withIdentifier: "newTransaction", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
guard let destination = segue.destination as? AddTransactionViewController else {
return
}
destination.member = member
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return member?.transactions?.count ?? 0
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableViewTransaction.dequeueReusableCell(withIdentifier: "transactionCell", for: indexPath)
if let transaction = member?.transactions?[indexPath.row] {
cell.textLabel?.text = transaction.reason
if let date = transaction.date {
cell.detailTextLabel?.text = dateFromatter.string(from: date)
}
}
return cell
}
@IBAction func sendEmail(_ sender: Any) {
let mailComposeVC = configureMailController()
if MFMailComposeViewController.canSendMail() {
self.present(mailComposeVC, animated: true, completion: nil)
} else {
showMailError()
}
}
func configureMailController() -> MFMailComposeViewController {
let mailComposerVC = MFMailComposeViewController()
mailComposerVC.mailComposeDelegate = self
mailComposerVC.setSubject("MAHNUNG")
mailComposerVC.setMessageBody("Hey, ich habe gesehen, dass dein Kontostand in der Buchhaltung zu tief ist. Ich bitte dich daher schnellst möglich mir das Geld zu bringen. Gruss", isHTML: false)
return mailComposerVC
}
func showMailError() {
let senMailErrorAlert = UIAlertController(title: "Could not send email", message: "Dein Gerät konnte die Email nicht senden.", preferredStyle: .alert)
let dismiss = UIAlertAction(title: "Ok", style: .default, handler: nil)
senMailErrorAlert.addAction(dismiss)
self.present(senMailErrorAlert, animated: true, completion: nil)
}
func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
controller.dismiss(animated: true, completion: nil)
}
}
addTransaction代码:
import UIKit
class AddTransactionViewController: UIViewController, UITextFieldDelegate {
@IBOutlet weak var reasonTextField: UITextField!
@IBOutlet weak var amountTextField: UITextField!
@IBOutlet weak var datePicker: UIDatePicker!
var member: Member?
override func viewDidLoad() {
super.viewDidLoad()
reasonTextField.delegate = self
amountTextField.delegate = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
reasonTextField.resignFirstResponder()
amountTextField.resignFirstResponder()
}
@IBAction func saveTransaction(_ sender: Any) {
let reason = reasonTextField.text
let date = datePicker.date
let moneyText = amountTextField.text ?? ""
let money = Double(moneyText) ?? 0.0
if let transaction = Trancsaction(money: money, reason: reason, date: date) {
member?.addToRawTransactions(transaction)
do {
try transaction.managedObjectContext?.save()
self.navigationController?.popViewController(animated: true)
} catch {
let alert = UIAlertController(title: "Transaktion konnte nicht gespeichert werden", message: "Bitte wiederholen Sie ihre Eingabe", preferredStyle: .alert)
present(alert, animated: true, completion: nil)
}
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
}
}
核心数据:
import UIKit
import CoreData
@objc(Trancsaction)
public class Trancsaction: NSManagedObject {
var date: Date? {
get {
return rawDate as Date?
}
set {
rawDate = newValue as NSDate?
}
}
convenience init?(money: Double, reason: String?, date: Date?) {
let appDelegate = UIApplication.shared.delegate as? AppDelegate
guard let context = appDelegate?.persistentContainer.viewContext else {
return nil
}
self.init(entity: Trancsaction.entity() , insertInto: context)
self.date = date
self.money = money
self.reason = reason
}
}