firebaseのfirestoreに追加したデータをtableviewに表示したいと考えています。
getSelectedEvent関数内でtableData配列にfirestoreのデータをいれるところまではできました。
しかし、tableView関数でデータを表示してもtableDataが空の状態でになってしまいます。
tableDataを関数の外でvar tableData:[String] = []と宣言しているのですが、位置が悪いのでしょうか。
どなたか、教えていただきたいです。
swift
1 2import UIKit 3import FSCalendar 4import CalculateCalendarLogic 5import FirebaseCore 6import FirebaseFirestore 7 8 9class ScheduleViewController: UIViewController, FSCalendarDelegate, FSCalendarDataSource, FSCalendarDelegateAppearance, UITableViewDelegate, UITableViewDataSource { 10 11 @IBOutlet weak var calendar: FSCalendar! 12 @IBOutlet weak var dateLabel: UILabel! 13 @IBOutlet weak var scheduleLabel: UILabel! 14 15 static var countEvent = 0 16 static var schedule = [String] (repeating: "", count: 50) 17 static var documentID = [String] (repeating: "", count: 50) 18 static var selectedDate = "" 19 var tableData:[String] = [] 20 21 var db: Firestore! 22 23 24 // public var selectedDate = "" 25 26 override func viewDidLoad() { 27 super.viewDidLoad() 28 29 // UILabelを複数行表示できるように設定 30 scheduleLabel.numberOfLines = 0 31 32 let settings = FirestoreSettings() 33 Firestore.firestore().settings = settings 34 db = Firestore.firestore() 35 36 // 最初は現在の日付を表示、スケジュールの登録もデフォルトは現在 37 let now = NSDate() 38 let dateFomatter = DateFormatter() 39 dateFomatter.dateFormat = "yyyy/MM/dd" 40 let today = dateFomatter.string(from: now as Date) 41 getSelectedEvent(today) 42 dateLabel.text = today 43 ScheduleViewController.selectedDate = today 44 45 self.calendar.dataSource = self 46 self.calendar.delegate = self 47 48 } 49 50 override func didReceiveMemoryWarning() { 51 super.didReceiveMemoryWarning() 52 // Dispose of any resources that can be recreated. 53 } 54 55 56 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 57 return self.tableData.count 58 } 59 60 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 61 let cell: UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) 62 // セルに表示する値を設定する 63 print(tableData) 64 cell.textLabel!.text = self.tableData[indexPath.row] 65 return cell 66 } 67 68 69 override func viewWillAppear(_ animated: Bool) { 70 let now = NSDate() 71 let dateFomatter = DateFormatter() 72 dateFomatter.dateFormat = "yyyy/MM/dd" 73 let today: Date = dateFomatter.date(from: dateFomatter.string(from: now as Date))! 74 self.calendar.today = today 75 } 76 77 78 79 80 fileprivate let gregorian: Calendar = Calendar(identifier: .gregorian) 81 fileprivate lazy var dateFormatter: DateFormatter = { 82 let formatter = DateFormatter() 83 formatter.dateFormat = "yyyy/MM/dd" 84 return formatter 85 }() 86 87 88 func calendar(_ calendar: FSCalendar, didSelect date: Date, at monthPosition: FSCalendarMonthPosition) { 89 let tmpDate = Calendar(identifier: .gregorian) 90 let year = tmpDate.component(.year, from: date) 91 let month = String(format: "%02d", tmpDate.component(.month, from: date)) 92 let day = String(format: "%02d", tmpDate.component(.day, from: date)) 93 let date = "(year)/(month)/(day)" 94 dateLabel.text = date 95 ScheduleViewController.selectedDate = date 96 getSelectedEvent(date) 97 } 98 99 100 101 // 選択された日付のイベントを取得、表示 102 func getSelectedEvent(_ selectedDay: String) { 103 var time = "" 104 var category = "" 105 var comment = "" 106 var info = "" 107 var events = "" 108 //var tableData:[String] = [] 109 db.collection("Taro") 110 .whereField("date", isEqualTo: selectedDay) 111 //.whereField("time", isLessThan: "18:30") 112 //.order(by: "time") 113 .getDocuments() { (QuerySnapshot, err) in 114 if let err = err { 115 print("Error getting documents: (err)") 116 self.scheduleLabel.text = "エラーだよ" 117 118 } else { 119 ScheduleViewController.countEvent = 0 120 for document in QuerySnapshot!.documents { 121 let sample: Dictionary = document.data() 122 time = sample["time"] as! String 123 category = sample["category"] as! String 124 comment = sample["comment"] as! String 125 info += time + " " + category + " " + comment + " " + "\n" 126 events = time + " " + category + " " + comment + " " 127 128 ScheduleViewController.documentID[ScheduleViewController.countEvent] = document.documentID 129 ScheduleViewController.schedule[ScheduleViewController.countEvent] = events 130 self.tableData.append(info) 131 132 } 133 self.scheduleLabel.text = info 134 print(self.tableData.count) 135 } 136 } 137 } 138} 139 140
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/09/08 05:03