gpt4 book ai didi

ios - Swift - 当我添加新单元格时,所有单元格都会发生变化(需要单个单元格)

转载 作者:行者123 更新时间:2023-11-30 12:49:11 27 4
gpt4 key购买 nike

我目前正在开发一个项目,需要一些帮助。我的应用已连接到 Firebase,并且它是用 Swift 编码的。

我的问题:

当我按下“共享”按钮时,它是否会将坐标发送到 Firebase(经度和纬度)。一个单元格添加到我的 UICollectionViewController 中并从 Firebase 检索坐标。因此它显示在 UICollectionViewCell 的 map 上。到目前为止,一切都很好。但是,当我从另一个位置再次按下“共享”按钮时,它会再次添加另一个单元格,但这一次它将所有单元格上的位置更改为最新位置。

即使我使用其他用户登录,它也会发生变化。我怀疑我必须将 func locationManager(_ manager: CLLocationManager, didUpdateLocationslocations: [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()



}

})


}


}

最佳答案

这是因为单元的可重复使用性。您需要以与设置名称相同的方法来设置位置,但请记住它们不应被新值覆盖。

关于ios - Swift - 当我添加新单元格时,所有单元格都会发生变化(需要单个单元格),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41227984/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com