Ios 当我想在UICollectionview中添加一个单元格时,它显示为two-Swift 3
我的应用程序有问题。当我想在Ios 当我想在UICollectionview中添加一个单元格时,它显示为two-Swift 3,ios,swift,firebase,uicollectionviewcell,Ios,Swift,Firebase,Uicollectionviewcell,我的应用程序有问题。当我想在UICollectionViewController中添加UICollectionViewcell时,它会显示两个单元格,而不是一个。他们的表现完全相同。还有其他人有同样的问题吗?我在谷歌上搜索了一下,想找到一个解决方案,但找不到什么。。。 我正在用Swift 3写作 这是我的密码: import UIKit import Firebase import MapKit import CoreLocation class Pro
UICollectionViewController
中添加UICollectionViewcell
时,它会显示两个单元格,而不是一个。他们的表现完全相同。还有其他人有同样的问题吗?我在谷歌上搜索了一下,想找到一个解决方案,但找不到什么。。。
我正在用Swift 3写作
这是我的密码:
import UIKit
import Firebase
import MapKit
import CoreLocation
class ProfileController: UICollectionViewController, UICollectionViewDelegateFlowLayout {
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(.childAdded, 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)
}
func ObservePosition() {
let ref = FIRDatabase.database().reference().child("position")
ref.observe(.childAdded, with: { (snapshot) in
if let dictionary = snapshot.value as? [String: AnyObject] {
let position = Position()
position.setValuesForKeys(dictionary)
self.positions.append(position)
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
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]()
var wiindow: UIWindow?
var mapView: MKMapView?
let locationManager = CLLocationManager()
let distanceSpan: Double = 500
var locationData: CLLocation!
override init(frame: CGRect) {
super.init(frame: frame)
setupViews()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
let nameLabel: UILabel = {
let label = UILabel()
label.font = UIFont.boldSystemFont(ofSize: 14)
label.translatesAutoresizingMaskIntoConstraints = false
return label
}()
let profileImag
eView: UIImageView = {
let imageView = UIImageView()
imageView.translatesAutoresizingMaskIntoConstraints = false
imageView.layer.cornerRadius = 22
imageView.layer.masksToBounds = true
imageView.backgroundColor = UIColor.blue
return imageView
}()
let separatorView: UIView = {
let view = UIView()
view.backgroundColor = UIColor(red: 192/255, green: 192/255, blue: 192/255, alpha: 1)
view.translatesAutoresizingMaskIntoConstraints = false
return view
}()
func setupViews() {
addSubview(profileImageView)
addSubview(nameLabel)
addSubview(separatorView)
addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-10-[v0(44)]-10-[v1]|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0": profileImageView, "v1": nameLabel]))
addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-10-[v0(44)]", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0": profileImageView]))
addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|[v0]-385-|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0": nameLabel]))
addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|[v0]|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0": separatorView]))
addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:[v0(1)]|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0": separatorView]))
self.wiindow = UIWindow(frame: UIScreen.main.bounds)
self.backgroundColor = UIColor(white: 0.95, alpha: 1)
self.mapView = MKMapView(frame: CGRect(x: 0, y: 70, width: (self.wiindow?.frame.width)!, height: 355))
self.addSubview(self.mapView!)
self.locationManager.delegate = self
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
self.locationManager.requestWhenInUseAuthorization()
self.locationManager.startUpdatingLocation()
self.mapView!.showsUserLocation = true
self.mapView!.isZoomEnabled = false
self.mapView!.isScrollEnabled = false
self.mapView!.isUserInteractionEnabled = false
}
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)
mapView.showsUserLocation = true
}
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let ref = FIRDatabase.database().reference().child("position")
ref.observe(.childAdded, with: { (locationSnap) in
if let locationDict = locationSnap.value as? [String: AnyObject] {
self.locationData = locations.last
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()
}
})
}
您有两个
observePosition
函数。在第二个步骤中,您将添加两次该职位。删除第二个观测位置
功能以修复它。您有两个观测位置
功能。在第二个步骤中,您将添加两次该职位。删除第二个observePosition
函数来修复它。是的,它工作了!我真的很想念你。。。但是谢谢!是的,成功了!我真的很想念你。。。但是谢谢!