2016-12-19 58 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

相关问题