Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Firebase Firestore getDocuments GeoPoint data
//
// ViewController.swift
// Futsal-Kakekomi
//
// Created by Hiro Fukami on 2019/04/22.
// Copyright © 2019 Hiro Fukami. All rights reserved.
//
import UIKit
import Firebase
// Model
struct Event {
let memo: String
let point: GeoPoint
let createdAt: Timestamp
init(memo: String, point: GeoPoint, createdAt: Timestamp) {
self.memo = memo
self.point = point
self.createdAt = createdAt
}
init(document: [String: Any]) {
memo = document["memo"] as? String ?? ""
point = document["point"] as? GeoPoint ?? GeoPoint(latitude: 0.0, longitude: 0.0)
createdAt = document["createdAt"] as! Timestamp
}
}
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource{
@IBOutlet weak var tableView: UITableView!
var events: [Event] = [] {
didSet {
tableView.reloadData()
}
}
var documentIDs: [String] = []
let db = Firestore.firestore()
//最初からあるコード
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.register (UINib(nibName: "CustomEventCell", bundle: nil),forCellReuseIdentifier: "repeatCell")
db.collection("events").getDocuments() { (querySnapshot, err) in
if let err = err {
print("Error getting documents: \(err)")
} else {
self.events = querySnapshot?.documents.map { Event(document: $0.data()) } ?? []
self.documentIDs = querySnapshot?.documents.map { $0.documentID } ?? []
print("document count: \(self.events.count)")
}
}
}
//最初からあるコード
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
//追加 セルの個数を指定するデリゲートメソッド(必須)
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.events.count
}
//追加 セルに値を設定するデータソースメソッド(必須)
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// セルを取得する
let cell = tableView.dequeueReusableCell(withIdentifier: "repeatCell", for: indexPath) as! CustomEventCell
let event = self.events[indexPath.row]
let documentID = documentIDs[indexPath.row]
cell.courtLabel.text = event.memo
return cell
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment