[解決したいこと]
Xcode11 swift5を使ってメモアプリを作っていますが、TableViewCell内のtextFieldの内容がFirestoreへ反映されないです。
TableViewController
import
1import Firebase 2import FirebaseFirestore 3 4class TableViewController: UITableViewController { 5 @IBOutlet var MainTableViewController: UITableView! 6 var db: Firestore! 7 var memoList = [MemoData] () 8 override func viewDidLoad() { 9 super.viewDidLoad() 10 db = Firestore.firestore() 11 12 13 if let storedData = UserDefaults().data(forKey: "memoList") { 14 do { 15 let unarchivedData = try NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(storedData) 16 memoList.append(contentsOf: unarchivedData as! [MemoData]) 17 } catch { 18 print(error) 19 } 20 } 21 } 22 // MARK: - Table view data source 23 24 override func numberOfSections(in tableView: UITableView) -> Int { 25 // #warning Incomplete implementation, return the number of sections 26 return 10 27 } 28 29 override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 30 // #warning Incomplete implementation, return the number of rows 31 return 3 32 } 33 34 override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 35 36 _ = Firestore.firestore() 37 38 if indexPath.row == 0 { 39 let cell = tableView.dequeueReusableCell(withIdentifier: "TableViewCell", for: indexPath) 40 return cell 41 }else { 42 if indexPath.row == 1 { 43 let cell = tableView.dequeueReusableCell(withIdentifier: "SecondTableViewCell", for: indexPath) 44 return cell 45 }else { 46 if indexPath.row == 2 { 47 let cell = tableView.dequeueReusableCell(withIdentifier: "ThirdTableViewCell", for: indexPath) 48 return cell 49 }else { 50 } 51 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 52 let cell = tableView.dequeueReusableCell(withIdentifier: "memoCell", for: indexPath) 53 let memoData = memoList[indexPath.row] 54 55 cell.textLabel?.text = memoData.memoTitle 56 return cell 57 } 58 59 func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) { 60 if editingStyle == .delete { 61 // Delete the row from the data source 62 memoList.remove(at: indexPath.row) 63 tableView.deleteRows(at: [indexPath], with: .fade) 64 let userDefalults = UserDefaults.standard 65 do { 66 let data: Data = try NSKeyedArchiver.archivedData(withRootObject: self.memoList, requiringSecureCoding: false) 67 userDefalults.set(data, forKey: "memoList") 68 userDefalults.synchronize() 69 } catch { 70 print(error) 71 } 72 } 73} 74 75 // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view 76 } 77 } 78 79 80 /* 81 // Override to support rearranging the table view. 82 override func tableView(_ tableView: UITableView, moveRowAt fromIndexPath: IndexPath, to: IndexPath) { 83 84 } 85 */ 86 87 /* 88 // Override to support conditional rearranging of the table view. 89 override func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool { 90 // Return false if you do not want the item to be re-orderable. 91 return true 92 } 93 */ 94 95 /* 96 // MARK: - Navigation 97 98 // In a storyboard-based application, you will often want to do a little preparation before navigation 99 override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 100 // Get the new view controller using segue.destination. 101 // Pass the selected object to the new view controller. 102 } 103 */ 104 105 return UITableViewCell() 106 } 107} 108 109コード
[TableViewCellController]
import
1 2class TableViewCell: UITableViewCell { 3 4 5 6 override func awakeFromNib() { 7 super.awakeFromNib() 8 // Initialization code 9 } 10 11 override func setSelected(_ selected: Bool, animated: Bool) { 12 super.setSelected(selected, animated: animated) 13 14 // Configure the view for the selected state 15 } 16 17} 18 19コード
[MemoData]
import
1import Firebase 2import FirebaseFirestore 3 4class MemoData: NSObject, NSCoding { 5 var memoTitle: String? 6 7 override init() { } 8 9 func encode(with aCoder: NSCoder) { 10 aCoder.encode(memoTitle, forKey: "memoTitle") 11 } 12 13 required init?(coder aDecoder: NSCoder) { 14 memoTitle = aDecoder.decodeObject(forKey: "memoTitle") as? String 15 } 16} 17 18コード
[AppleDelegate]
import
1import Firebase 2import FirebaseFirestore 3 4 5@UIApplicationMain 6class AppDelegate: UIResponder, UIApplicationDelegate { 7 8 9 10 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { 11 FirebaseApp.configure() 12 13 14 15 _ = Firestore.firestore() 16 17 return true 18 } 19 20 // MARK: UISceneSession Lifecycle 21 22 func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration { 23 // Called when a new scene session is being created. 24 // Use this method to select a configuration to create the new scene with. 25 return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role) 26 } 27 28 func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) { 29 // Called when the user discards a scene session. 30 // If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions. 31 // Use this method to release any resources that were specific to the discarded scenes, as they will not return. 32 } 33 34 35} 36 37 38コード
試したこと
・違う方法でデータを追加するソースコードに変える。
あなたの回答
tips
プレビュー