Ich arbeite gerade an einem Projekt und brauche Hilfe. Ich habe meine App mit Firebase verbunden und sie ist in Swift codiert.Swift - Alle Zellen ändern sich, wenn ich eine neue hinzufüge (will einzelne Zellen)
Mein Problem:
Wenn ich drücken Sie die Schaltfläche „Teilen“ macht es die Koordinaten Firebase (Länge und Breite) senden. Eine Zelle fügt meine UICollectionViewController
hinzu und ruft die Koordinaten von Firebase ab. So zeigt es sich auf einer Karte in der UICollectionViewCell
. So weit, ist es gut. Aber wenn ich die Taste "Teilen" erneut von einer anderen Position aus drücke, fügt sie erneut eine Zelle hinzu, aber dieses Mal ändert sie die Position aller Zellen auf die letzte Position.
Auch wenn ich mit einem anderen Benutzer angemeldet bin, ändert sich das. Ich vermutet, dass ich die func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
in
override func collectionView(_ collectionView: UICollectionView, cellForItemAt
indexPath: IndexPath) -> UICollectionViewCell
hoffe, euch diese verstehen können setzen haben. Es ist ein bisschen schwierig, dieses Ding gut zu machen, ziemlich neu in diesem Ding.
PS: Der Code der beste nicht sein kann, aber ich helfen will nur mit, was ich fragen ...
ist hier mein Code
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)
}
Und meine Zelle
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()
}
})
}
}
Setzen Sie einen Haltepunkt in dieser Zeile: 'let position = positions [(indexPath as NSIndexPath) .row]'. Was ist in deinen "Positionen"? Sind es die Daten, die Sie erwarten? Sind es mehrere verschiedene Standorte? – Bek
Was ist positions.fromId? ändert sich das? –
Ich bekomme 8 Werte innerhalb der Position, ich habe 8 verschiedene Werte/Positionen in Firebase Storage. Und es findet "Länge" und "Breite" als NSNumber. Nur um darauf hinzuweisen, dass der Code in 'cellForItemAt indexPath' nur dazu dient, den Benutzernamen zu erhalten, der die "Zelle" gepostet hat, und es funktioniert perfekt. – Stevic