对于冗长的帖子感到抱歉!
我有一个带2个单元格的UICollectionView(newJobCellGeneral& newJobCellTechnical)。
我正在使用通知从每个单元格向Firebase写入数据。在我的UICollectionView的第二页上按下按钮之前,不会写入数据。
当按下该按钮时,通知会激活newJobCellGeneral中使用autoId将数据写入Firebase的操作。
我想要做的是将newJobCellGeneral中创建的autoId引用到我的newJobCellTechnical动作中,所以当我稍后引用数据时,我可以将它们全部组合在一起,因为它们都具有相同的Id。
我不确定这是否是最好的方法,但这是我认为可以完成的唯一方法,因为我还是Xcode&火力
这里是我想要创建的所需JSON设置:
{
"jobInfo" : {
"-L61ATfMsTg1RH2T90tV" : {
"agencyName" : "Ben",
"directorName" : "Ben",
"jobBrand" : "Ben",
"jobName" : "Ben",
"prodCoName" : "Ben"
}
},
"jobTechnical" : {
"-L61ATfTZojWmlkB7paU" : {
"jobId" : "L61ATfMsTg1RH2T90tV"
"FPS" : "34",
"resolutionHeight" : "1080",
"resolutionWidth" : "1920"
}
},
"shots" : {
"-L61ATfTZojWmlkB7paV" : {
"frame_001" : {
"jobId" : "L61ATfMsTg1RH2T90tV"
"FPS" : "34",
"resolutionHeight" : "1080",
"resolutionWidth" : "1920"
},
"frame_002" : {
"jobId" : "L61ATfMsTg1RH2T90tV"
"FPS" : "34",
"resolutionHeight" : "1080",
"resolutionWidth" : "1920"
},
"frame_003" : {
"jobId" : "L61ATfMsTg1RH2T90tV"
"FPS" : "34",
"resolutionHeight" : "1080",
"resolutionWidth" : "1920"
}
}
}
}
这是UICollectionView:
class page_newJobSwipingController : UICollectionViewController, UICollectionViewDelegateFlowLayout, UIImagePickerControllerDelegate, UINavigationControllerDelegate, UIPickerViewDelegate, UITextFieldDelegate {
// VARIABLES
var ref:DatabaseReference?
// BOTTOM BUTTONS
private let previousButton: UIButton = {
let button = UIButton(type: .system)
button.setTitle("Previous", for: .normal)
button.titleLabel?.font = UIFont.boldSystemFont(ofSize: 14)
button.setTitleColor(.gray, for: .normal)
button.addTarget(self, action: #selector(handlePrev), for: .touchUpInside)
button.translatesAutoresizingMaskIntoConstraints = false
return button
}()
private let nextButton: UIButton = {
let button = UIButton(type: .system)
button.setTitle("Next", for: .normal)
button.titleLabel?.font = UIFont.boldSystemFont(ofSize: 14)
let pinkColour = UIColor(red: 232/255, green: 68/266, blue: 133/255, alpha: 1)
button.setTitleColor(.mainPink, for: .normal)
button.translatesAutoresizingMaskIntoConstraints = false
button.addTarget(self, action: #selector(handleNext), for: .touchUpInside)
return button
}()
// SET UP NEXT AND PREVIOUS BUTTONS TO HAVE A FUNCTION
@IBAction func handlePrev(sender : UIButton) {
let prevIndex = max(pageControl.currentPage - 1, 0)
pageControl.currentPage = prevIndex
let indexPath = IndexPath(item: prevIndex, section: 0)
collectionView?.scrollToItem(at: indexPath, at: .centeredHorizontally, animated: true)
}
@IBAction func handleNext(sender : UIButton) {
let nextIndex = pageControl.currentPage + 1
pageControl.currentPage = nextIndex
if nextIndex == 1 {
print ("move to page 2")
} else {
print ("send alert message")
NotificationCenter.default.post(name: Notification.Name("handleNewJobGeneral"), object: nil)
NotificationCenter.default.post(name: Notification.Name("handleNewJobTechnical"), object: nil)
storyboardAlert()
}
let indexPath = IndexPath(item: 1, section: 0)
collectionView?.scrollToItem(at: indexPath, at: .centeredHorizontally, animated: true)
}
// HANDLE UPLOAD STORYBOARD OPTIONS
@IBAction func storyboardAlert() {
let imagePickerController = UIImagePickerController()
imagePickerController.delegate = self
// ACTION SHEET FOR ADDING NEW ATTACHMENT
let alert = UIAlertController(title: "New job is about to be created", message: "Do you want to upload storyboard cells now?", preferredStyle: .actionSheet)
alert.addAction(UIAlertAction(title: "Now", style: .default, handler: { (action:UIAlertAction) in
let storyboardUpload = page_newJobStoryboardUpload()
self.show(storyboardUpload, sender: self)
}))
alert.addAction(UIAlertAction(title: "Later", style: .default, handler: { (action:UIAlertAction) in
let jobList = page_jobList()
self.show(jobList, sender: self)
}))
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
self.present(alert, animated: true, completion: nil)
}
// PAGE CONTROL
private let pageControl: UIPageControl = {
let pc = UIPageControl()
pc.numberOfPages = 2
pc.currentPageIndicatorTintColor = .mainPink
pc.pageIndicatorTintColor = UIColor(red: 249/255, green: 207/266, blue: 224/255, alpha: 1)
return pc
}()
override func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
let x = targetContentOffset.pointee.x
pageControl.currentPage = Int(x / view.frame.width)
}
// CONSTRAINTS OF BOTTOM CONTROLS
fileprivate func setupBottomControls(){
let bottomControlsStackView = UIStackView(arrangedSubviews: [previousButton, pageControl, nextButton])
bottomControlsStackView.translatesAutoresizingMaskIntoConstraints = false
bottomControlsStackView.distribution = .fillEqually
view.addSubview(bottomControlsStackView)
NSLayoutConstraint.activate([
bottomControlsStackView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor),
bottomControlsStackView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
bottomControlsStackView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
bottomControlsStackView.heightAnchor.constraint(equalToConstant: 50)
])
}
// SUPER VIEW DID LOAD
override func viewDidLoad() {
super.viewDidLoad()
collectionView?.backgroundColor = .white
collectionView?.register(newJobCellGeneral.self, forCellWithReuseIdentifier: "newJobCellGeneral")
collectionView?.register(newJobCellTechnical.self, forCellWithReuseIdentifier: "newJobCellTechnical")
collectionView?.isPagingEnabled = true
setupBottomControls()
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 0
}
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 2
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if indexPath.item == 0 {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "newJobCellGeneral", for: indexPath) as! newJobCellGeneral
navigationItem.title = "General Info"
return cell
} else {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "newJobCellTechnical", for: indexPath) as! newJobCellTechnical
navigationItem.title = "Technical Specs"
return cell
}
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: view.frame.width, height: view.frame.height)
}
}
这是newJobCellGeneral初始化(这里还有其他的东西,只是想显示通知)和动作(我不会上传剩下的代码,因为它很长 - 除非它是必需的):
override init(frame: CGRect) {
super.init(frame: frame)
NotificationCenter.default.addObserver(self, selector: #selector(handleNewJobGeneral), name: NSNotification.Name(rawValue: "handleNewJobGeneral"), object: nil)
}
// HANDLE NEW JOB GENERAL
@objc func handleNewJobGeneral(){
let newJobBrand = jobBrand.text!
let newJobName = jobName.text!
let newDirectorName = directorName.text!
let newAgencyName = agencyName.text!
let newProdCoName = prodCoName.text!
// WHERE TO PUT IN DATABASE
let jobInfoReference = Database.database().reference().child("jobInfo")
let jobInfoChildRef = jobInfoReference.childByAutoId()
// REFERENCING DICTIONARY
let jobBrandValue = ["jobBrand": newJobBrand]
let jobNameValue = ["jobName": newJobName]
let jobDirectorValue = ["directorName": newDirectorName]
let jobAgencyNameValue = ["agencyName": newAgencyName]
let jobProdCoValue = ["prodCoName": newProdCoName]
// WRITE TO DATABASE
jobInfoChildRef.updateChildValues(jobBrandValue)
jobInfoChildRef.updateChildValues(jobNameValue)
jobInfoChildRef.updateChildValues(jobDirectorValue)
jobInfoChildRef.updateChildValues(jobAgencyNameValue)
jobInfoChildRef.updateChildValues(jobProdCoValue)
}
这是newJobCellGeneral初始化(这里还有其他的东西,只是想显示通知)和动作(我不会上传剩下的代码,因为它很长 - 除非它是必需的):
override init(frame: CGRect) {
super.init(frame: frame)
NotificationCenter.default.addObserver(self, selector: #selector(handleNewJobTechnical), name: NSNotification.Name(rawValue: "handleNewJobTechnical"), object: nil)
}
@IBAction func handleNewJobTechnical(){
//let numberOfShootDaysAmount = numberOfShootDays.text!
let FPS = FPSamount.text!
let resolutionWidth = resolutionWidthAmount.text!
let resolutionHeight = resolutionHeightAmount.text!
let shotCount = shotAmount.text!
// WHERE TO PUT IN DATABASE
let jobTechnicalReference = Database.database().reference().child("jobTechnical")
let jobTechnicalChildRef = jobTechnicalReference.childByAutoId()
let shotListReference = Database.database().reference().child("shots")
let shotListChildRef = shotListReference.childByAutoId()
// REFERENCING DICTIONARY
let FPSValue = ["FPS": FPS]
let resolutionWidthValue = ["resolutionWidth": resolutionWidth]
let resolutionHeightValue = ["resolutionHeight": resolutionHeight]
// ADD TO TECHNICAL JOB
jobTechnicalChildRef.updateChildValues(FPSValue)
jobTechnicalChildRef.updateChildValues(resolutionWidthValue)
jobTechnicalChildRef.updateChildValues(resolutionHeightValue)
// FOR LOOP VARIABLES
let shotCountTotal:Int = (Int(shotCount)! + 1)
for i in 1 ..< shotCountTotal {
let frame = "frame_"
let number = (String(format: "%03d", (i)))
let frameNumber = (frame + number)
let frameListChildRef = shotListChildRef.child(frameNumber)
// WRITE TO FRAMES DATABASE
frameListChildRef.updateChildValues(FPSValue)
frameListChildRef.updateChildValues(resolutionWidthValue)
frameListChildRef.updateChildValues(resolutionHeightValue)
}
}
感谢所有能帮助我理解这一点的人!