我目前正在开展一个项目,我需要一些帮助。我将我的应用程序连接到Firebase,并使用Swift进行编码。
我的问题:
当我按下“共享”按钮时,它会将坐标发送到Firebase(经度和纬度)。我的UICollectionViewController
中添加了一个单元格,并从Firebase中检索坐标。所以它显示在UICollectionViewCell
的地图上。到现在为止还挺好。但是当我再次从另一个位置按下“共享”按钮时,它会再次添加另一个单元格,但这次它将所有单元格上的位置更改为最新位置。
即使我已与其他用户登录,也会更改。我怀疑我必须将func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
放入
override func collectionView(_ collectionView: UICollectionView, cellForItemAt
indexPath: IndexPath) -> UICollectionViewCell
希望你们能理解这一点。做这件事有点难,这个东西很新。
PS:代码可能不是最好的,但我只是希望得到我所要求的帮助......
这是我的代码
import UIKit
import Firebase
import MapKit
import CoreLocation
class ProfileController: UICollectionViewController, UICollectionViewDelegateFlow
Layout {
let cellId = "cellId"
var users = [User]()
var positions = [Position]()
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Logout", style: .plain, target: self, action: #selector(handleLogout))
navigationItem.title = "Profile"
collectionView?.backgroundColor = UIColor(white: 0.95, alpha: 1)
collectionView?.alwaysBounceVertical = true
collectionView?.register(FeedCell.self, forCellWithReuseIdentifier: cellId)
observePosition()
}
func handleLogout() {
do {
try FIRAuth.auth()?.signOut()
} catch let logoutError {
print(logoutError)
}
let loginContoller = LoginController()
present(loginContoller, animated: true, completion: nil)
}
func observePosition() {
let ref = FIRDatabase.database().reference().child("position")
ref.observe(.child
Added, with: { (snapshot) in
if let dictionary = snapshot.value as? [String: AnyObject] {
let position = Position()
position.setValuesForKeys(dictionary)
self.positions.append(position)
DispatchQueue.main.async(execute: {
self.collectionView!.reloadData()
})
}
}, withCancel: nil)
}
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return positions.count
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt
indexPath: IndexPath) -> UICollectionViewCell {
let FedCell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! FeedCell
let position = positions[(indexPath as NSIndexPath).row]
if let fromId = position.fromId { //To get the username
let ref = FIRDatabase.database().reference().child("users").child(fromId) //get username
ref.observeSingleEvent(of: .value, with: { (snapshot) in
if let dictionary = snapshot.value as? [String: AnyObject] {
FedCell.nameLabel.text = dictionary["name"] as? String //get username
}
}, withCancel: nil)
}
return FedCell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: view.frame.width, height: 450)
}
和我的手机
class FeedCell: UICollectionViewCell, UICollectionViewDelegateFlowLayout, CLLocationManagerDelegate, MKMapViewDelegate {
var user = [User]()
var positions = [Position]()
private func locationManager(manager: CLLocationManager, didUpdateToLocation newLocation: CLLocation, fromLocation oldLocation: CLLocation) {
if let mapView = self.mapView {
let region = MKCoordinateRegionMakeWithDistance(newLocation.coordinate, self.distanceSpan, self.distanceSpan)
mapView.setRegion(region, animated: true)
}
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let ref = FIRDatabase.database().reference().child("position")
ref.obse
rve(.childAdded, with: { (locationSnap) in
if let locationDict = locationSnap.value as? [String: AnyObject] {
guard let lat = locationDict["latitude"] as? CLLocationDegrees,
let long = locationDict["longitude"] as? CLLocationDegrees else { return }
let center = CLLocationCoordinate2D(latitude: lat, longitude: long)
let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01))
let locationPin = CLLocationCoordinate2D(latitude: lat, longitude: long)
let annotation = MKPointAnnotation()
annotation.coordinate = locationPin
self.mapView!.setRegion(region, animated: true)
self.mapView!.showsUserLocation = false
self.mapView!.addAnnotation(annotation)
self.mapView!.showAnnotations([annotation], animated: true)
self.locationManager.stopUpdatingLocation()
}
})
}
}
答案 0 :(得分:0)
这是因为细胞的可重复使用。 您需要在设置名称的同一方法中设置位置,并记住它们不应覆盖新值。