class PeopleVC: UIViewController {
//MARK:-
@IBOutlet weak var tableView: UITableView!
var peoples = [People]()
var category: Category!
var listener: ListenerRegistration!
var db: Firestore!
var locationManager: CLLocationManager!
//let locationManager = CLLocationManager()
//MARK:- viewLifecycleMethods
override func viewDidLoad() {
super.viewDidLoad()
db = Firestore.firestore()
setupTableView()
// checkLocationServices()
}
override func viewWillAppear(_ animated: Bool) {
fetchCollection()
//setPeoples()
}
override func viewWillDisappear(_ animated: Bool) {
listener.remove()
}
fileprivate func setupTableView() {
tableView.dataSource = self
tableView.delegate = self
tableView.register(UINib(nibName: Identifiers.PeopleCell, bundle: nil), forCellReuseIdentifier: Identifiers.PeopleCell)
tableView.rowHeight = UITableView.automaticDimension
tableView.estimatedRowHeight = 200
}
}
//MARK:- UITableViewDataSource and UITableViewDelegate
extension PeopleVC: UITableViewDataSource, UITableViewDelegate {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return peoples.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if let cell = tableView.dequeueReusableCell(withIdentifier: Identifiers.PeopleCell, for: indexPath) as? PeopleCell {
cell.configureCell(people: peoples[indexPath.row])
return cell
}
return UITableViewCell()
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let vc = PeopleDetailVC()
let selectedPeople = peoples[indexPath.row]
vc.people = selectedPeople
vc.modalPresentationStyle = .overCurrentContext
vc.modalTransitionStyle = .crossDissolve
present(vc, animated: true, completion: nil)
}
// func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
//
// return tableView.estimatedRowHeight
// }
// func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
//
// print(preferredContentSize.height)
// print(tableView.contentSize.height)
// print(tableView.contentSize)
//
// return 300
// }
}
//MARK:- getPeopleMethods
extension PeopleVC {
// By usinf the Reference Fetch the data
func fetchCollection() {
let collectionRef = db.collection("peoples").whereField("category", isEqualTo: category.id)
listener = collectionRef.addSnapshotListener { (snap, error) in
if let error = error {
print(error.localizedDescription)
return
}
self.peoples.removeAll()
guard let documents = snap?.documents else { return }
for document in documents {
let data = document.data()
let newPeople = People(data: data)
self.peoples.append(newPeople)
}
self.tableView.reloadData()
}
}
}
Be the first to comment.