let db = Firestore.firestore() let user = Auth.auth().currentUser
let tableView = UITableView()
var locationArray = [String]()
var titleArray = [String]()
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .systemGray6
tableView.delegate = self
tableView.dataSource = self
tableView.backgroundColor = .systemGray6
navigationController?.navigationBar.isHidden = true
setupUI()
fetchFirestoreLocationData()
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
tableView.frame.size.height = tableView.contentSize.height
}
func fetchFirestoreLocationData() {
let ref = db.collection("savedPlaces").whereField("userID", isEqualTo: user!.uid)
ref.addSnapshotListener { [weak self] snapshot, error in
guard let self = self else { return }
if let error = error {
print(error)
return
}
guard let documents = snapshot?.documents else {
print("no document.")
return
}
self.titleArray.removeAll(keepingCapacity: false)
self.locationArray.removeAll(keepingCapacity: false)
for document in documents {
if let latitudeStr = document.get("latitude") as? String,
let longitudeStr = document.get("longitude") as? String,
let title = document.get("title") as? String {
self.titleArray.append(title)
self.locationArray.append("\(latitudeStr), \(longitudeStr)")
}
}
print(titleArray)
self.tableView.reloadData()
}
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return titleArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell()
var content = cell.defaultContentConfiguration()
content.text = titleArray[indexPath.row]
cell.contentConfiguration = content
return cell
}
When print command executed, locationArray is not empty but i can't see titles on tableView.
I tried to run function on viewWillAppear and viewDidAppear but result hasn't changed. I pick locations from mapView and save it to firestore, and i want to fetch location data and list locations in tableView.