2016-12-19 96 views
0

我目前正在開發一個項目,我需要一些幫助。我已將我的應用連接到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() 



     } 

    }) 


    } 


} 
+0

在這一行放置一個斷點:'let position = positions [(indexPath as NSIndexPath).row]'。你的'職位'裏面有什麼?這是你期望的數據嗎?這是幾個不同的地點? – Bek

+0

什麼是positions.fromId?這是否改變? –

+0

我在內部獲得8個值,在Firebase存儲中我擁有8個不同的值/位置。它發現「經度」和「緯度」爲NSNumber。只需要指出'cellForItemAt indexPath'中的代碼只是爲了獲得張貼「單元格」的用戶名,它的工作原理是完美的。 – Stevic

回答

0

這是因爲細胞的可重複使用性。 您需要使用與設置名稱相同的方法來設置位置,同時請記住,它們不應該用新值覆蓋。

+0

好的,現在我已經在設置名稱的相同方法中設置了位置。它仍然在做同樣的事情,但我認爲它們會覆蓋一個新的值(?)如何修復,以便不會覆蓋新值?對不起,但我在Swift中很新,所以我需要幫助。已經搜索了一下但不能理解。 – Stevic

+0

創建模型類來存儲需要在單元格中顯示的數據會更好。並從模型類填充您的單元格,這將不會覆蓋舊值。 –

+0

好的,問題是我不明白我會怎麼做。正如我所說,我在Swift中很新... – Stevic

相關問題