我目前正在开发一个项目,我需要一些帮助。我已将我的应用连接到Firebase,并使用Swift编码。Swift - 当我添加一个新单元(想要单个单元格)时,所有单元格都会更改
我的问题:
当我按下“共享”按钮做它的坐标发送到火力地堡(经度和纬度)。一个单元格添加到我的UICollectionViewController
中,并从Firebase中检索坐标。所以它出现在UICollectionViewCell
的地图上。到现在为止还挺好。但是当我再次从另一个位置按下“Share”按钮时,它会再次添加另一个单元格,但这次它会将所有单元格上的位置更改为最新位置。
即使我用另一个用户登录,它也会改变。我怀疑,我必须把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()
}
})
}
}
在这一行放置一个断点:'let position = positions [(indexPath as NSIndexPath).row]'。你的'职位'里面有什么?这是你期望的数据吗?这是几个不同的地点? – Bek
什么是positions.fromId?这是否改变? –
我在内部获得8个值,在Firebase存储中我拥有8个不同的值/位置。它发现“经度”和“纬度”为NSNumber。只需要指出'cellForItemAt indexPath'中的代码只是为了获得张贴“单元格”的用户名,它的工作原理是完美的。 – Stevic