Search code examples
swiftfirebase-realtime-databaseuitextfielduid

Create a node named a user's uid in firebase database from a textfield that displays their name


I am sending information to my firebase database via textfields. I would like to make one of the nodes Users and then under that node different nodes separated by a user's uid. Under each UID node there would be three datapoint from textfields that are filled out from three textfields on the custom cell. I want to be able to type the user's name in the name text field and have it send their respective UID. I currently have the following code creating this:

database now

@IBAction func sendButtonTapped(_ sender: Any) {
    
    let companyNameC = companyNameTextFieldConsiderations.text!.trimmingCharacters(in: .whitespacesAndNewlines)
    let companyDescriptionC = companyDescriptionTextFieldConsiderations.text!.trimmingCharacters(in: .whitespacesAndNewlines)
    
      let today = Date()
            let formatter1 = DateFormatter()
            formatter1.dateFormat = "MMM d y"
            print(formatter1.string(from: today))
            let todaysDate = formatter1.string(from: today)
            
            let storageRef = Storage.storage().reference(forURL: "I have my code here")
            let imageName = companyNameTextFieldConsiderations.text!
            let storageCompanyRef = storageRef.child("Company_Image_Considerations").child("\(todaysDate)").child(imageName)
            let companyDescriptionTextFieldText = companyDescriptionTextFieldConsiderations.text
            let dateToStart = startDateTextFieldConsiderations.text
            let dateToDecide = endDateTextFieldConsiderations.text
            let companyRef = Database.database().reference().child("Considerations").child("\(todaysDate)").child(imageName)
    let considerationInfluencerRef = Database.database().reference().child("Considerations").child("\(todaysDate)").child(imageName).child("Users")
            let cell = tableView.dequeueReusableCell(withIdentifier: AddPersonCell) as! ConsiderationsCell
            let nameTFC = cell.nameTextFieldConsiderations.text!
            let feedTFC = cell.feedTextFieldConsiderations.text
            let storyTFC = cell.storyTextFieldConsiderations.text
            let compensationTFC = cell.compensationTextFieldConsiderations.text
            let values = ["Name": nameTFC]
    
            guard let imageSelected = self.CompanyImage.image else {
                               print ("Avatar is nil")
                               return
                           }
                   
                               var dict: Dictionary<String, Any> = [
                                  "Company Image": "",
                                   "Company Description": companyDescriptionTextFieldText!,
                                   "Start Date": dateToStart,
                                   "Decision Date": dateToDecide
                              ]
    
                   
                           guard let imageData = imageSelected.jpegData(compressionQuality: 0.5) else {
                               return
                           }
    
                           let metadata = StorageMetadata()
                           metadata.contentType = "image/jpeg"
                           storageCompanyRef.putData(imageData, metadata: metadata, completion:
                               { (StorageMetadata, error) in
                                   if (error != nil) {
                                       return
                                   }
                                
                                   storageCompanyRef.downloadURL { (url, error) in
                                       if let metadateImage = url?.absoluteString {
                                           dict["Company Image"] = metadateImage
                   
                                           companyRef.updateChildValues(dict, withCompletionBlock:  {
                                               (error, ref) in
                                               if error == nil {
                                                   print("Done")
                                                   return
                                              }
                   
                                               }
                   
                                           )
                                       }
                                   }
                   
                                   storageRef.updateMetadata(metadata) { metadata, error in
                                       if error != nil {
                                        //Uh-oh, an error occurred!
                                     } else {
                                       // Updated metadata for 'images/forest.jpg' is returned
                                     }
                                   }
                           })

    considerationInfluencerRef.updateChildValues(values as [AnyHashable : Any]) { (error, ref) in
    if error != nil {
        print(error ?? "")
        return
    }
    
                                self.navigationController?.popViewController(animated: true)
                   
                           }
}

I want it to look like this:

preferred database

Edit:

Here is an image of the dynamic prototype cell:

cell

You're saying is to use the name textfield to name an object in an array. That object has it's own array that would consist of the two textfields with # and the textfield with $?

EDIT 2:

import UIKit

class ConsiderationCellModel: NSObject {
var feedNumberQauntity: String?
var storyNumberQuantity: String?
var compensationAmmount: String?

}


Solution

  • I solved one of my problems. I needed to change the line of code:

    let cell = tableView.dequeueReusableCell(withIdentifier: AddPersonCell) as! ConsiderationsCell
    

    I needed to use cellForRow

    let index = IndexPath(row:0,section:0)
    guard let cell = tableView.cellForRow(at:index) as? ConsiderationsCell else { print("Not shown") ; return }
    

    This only solved one of my problems. Now I need to make it work for multiple rows.