title: Use a table to display users added and their data
body: <p>I am new to coding and have been trying to figure out the current problem I have for the last few weeks.</p>
  <p>I have posted a bunch asking different variations of the same question and was informed the way I was trying to execute my idea was incorrect.</p>
  <p>I am trying to send a collection of data to a list of users. The data involves a company name, the company's image, the day they need to make a decision and the day it was sent. I am currently able to send that information to my firebase database and firebase storage for the company image. The company image is also stored as a string under the company's information node.</p>
  <p>What I was originally trying to accomplish was being able to create cells by pushing a button. The table would populate a cell with four textfields. A name textfield would be where I selected the user. Their firebase database name would show up in the textfield. I then could input text in three other textfields that corresponded to only that user. The user would be able to see the company info and the dates but then only their specific row data. I was told that I shouldn't be doing that and I decided to venture off to another possible solution.</p>
  <p>Situation Now:</p>
  <p>I have the same view controller set up with the company information and add button. That looks like:</p>
  <p><a href="https://i.stack.imgur.com/q78Pn.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/q78Pn.png" alt="viewcontroller" /></a></p>
  <p>When the add button is pressed, a new view controller is shown with four textfields. The view controller name textfield allows for the user to select a user from the database and the text changes to that persons name. It looks like this:</p>
  <p><a href="https://i.stack.imgur.com/yVAgO.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/yVAgO.png" alt="viewcontroller2" /></a></p>
  <p>I am using the name textfield to append an array which then is being used for the table in the first view controller to determine the number of rows. It is currently appending the array but not populating any cells. I plan on using labels to save the user and the other 3 textfields to the same row. But I can't even get any cells to populate.</p>
  <p>Here is the code for the view controller that will display the users and their info in the table below the company information:</p>
  <pre><code>import UIKit
  import Firebase
  
  class ConsiderationsTestViewController: UIViewController, UITextFieldDelegate {
  
  @IBOutlet weak var tableView: UITableView!
  
  static var numberOfPeople: [String] = []
  
  var AddPersonCell = &quot;AddPersonCell&quot;
  
  @IBOutlet weak var CompanyImage: UIImageView!
  
  @IBOutlet weak var companyNameTextFieldConsiderations: UITextField!
  
  @IBOutlet weak var companyDescriptionTextFieldConsiderations: UITextField!
  
  @IBOutlet weak var startDateTextFieldConsiderations: UITextField!
  
  @IBOutlet weak var endDateTextFieldConsiderations: UITextField!
  
  let datePickerS = UIDatePicker()
  let datePickerE = UIDatePicker()
  
      var database: Database!
      var storage: Storage!
      var selectedImage: UIImage?
      
      var ref:DatabaseReference?
      var databaseHandle:DatabaseHandle = 0
      
      let dbref = Database.database().reference()
      let uid = Auth.auth().currentUser?.uid
      
      override func viewDidLoad() {
          super.viewDidLoad()
          
          tableView.delegate = self
          tableView.dataSource = self
          
          self.companyNameTextFieldConsiderations.delegate = self
          self.companyDescriptionTextFieldConsiderations.delegate = self
          
          // Set the Firebase reference
          ref = Database.database().reference()
          
          let tapGesture = UITapGestureRecognizer(target: self, action: #selector(ConsiderationsTestViewController.handleSelectCompanyImageView))
           CompanyImage.addGestureRecognizer(tapGesture)
           CompanyImage.isUserInteractionEnabled = true
  
          self.navigationController!.navigationBar.isTranslucent = false
          
          navigationItem.backBarButtonItem = UIBarButtonItem(
          title: &quot;&quot;, style: .plain, target: nil, action: nil)
          
          createDatePickerForStart()
          
          createDatePickerForEnd()
          
      }
  
      @objc func handleSelectCompanyImageView() {
  
          let pickerController = UIImagePickerController()
          pickerController.delegate = self
          pickerController.allowsEditing = true
          present(pickerController, animated: true, completion: nil)
                      }
      
  @IBAction func AddPersonTapped(_ sender: Any) {
    
  }
  
  @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 = &quot;MMM d y&quot;
      print(formatter1.string(from: today))
      let todaysDate = formatter1.string(from: today)
              
      let storageRef = Storage.storage().reference(forURL: &quot; MY STORAGE URL HERE&quot;)
      let imageName = companyNameTextFieldConsiderations.text!
      let storageCompanyRef = storageRef.child(&quot;Company_Image_Considerations&quot;).child(&quot;\(todaysDate)&quot;).child(imageName)
      let companyDescriptionTextFieldText = companyDescriptionTextFieldConsiderations.text
      let dateToStart = startDateTextFieldConsiderations.text
      let dateToDecide = endDateTextFieldConsiderations.text
      let companyRef = Database.database().reference().child(&quot;Considerations&quot;).child(&quot;\(todaysDate)&quot;).child(imageName)
      
      let index = IndexPath(row: 0,section: 0)
      let cell = tableView.cellForRow(at:index) as! ConsiderationsCell
      let nameTFC = cell.nameTextFieldConsiderations.text!.trimmingCharacters(in: .whitespacesAndNewlines)
      let feedTFC = cell.feedTextFieldConsiderations.text!.trimmingCharacters(in: .whitespacesAndNewlines)
      let storyTFC = cell.storyTextFieldConsiderations.text!.trimmingCharacters(in: .whitespacesAndNewlines)
      let compensationTFC = cell.compensationTextFieldConsiderations.text!.trimmingCharacters(in: .whitespacesAndNewlines)
      
      let values = [&quot;Feed_Quantity&quot;: feedTFC, &quot;Story_Quantity&quot;: storyTFC, &quot;Compensation&quot;: compensationTFC]
      let considerationInfluencerRef = Database.database().reference().child(&quot;Considerations&quot;).child(&quot;\(todaysDate)&quot;).child(imageName).child(&quot;Users&quot;).child(&quot;\(nameTFC)&quot;)
      
          guard let imageSelected = self.CompanyImage.image else {
              print (&quot;Avatar is nil&quot;)
              return
                  }
                     
                  var dict: Dictionary&lt;String, Any&gt; = [
                      &quot;Company Image&quot;: &quot;&quot;,
                      &quot;Company Description&quot;: companyDescriptionTextFieldText!,
                      &quot;Start Date&quot;: dateToStart!,
                      &quot;Decision Date&quot;: dateToDecide! ]
      
                     
          guard let imageData = imageSelected.jpegData(compressionQuality: 0.5) else {
              return
                  }
      
                  let metadata = StorageMetadata()
                  metadata.contentType = &quot;image/jpeg&quot;
                  storageCompanyRef.putData(imageData, metadata: metadata, completion:
                  { (StorageMetadata, error) in
                  if (error != nil) {
                  return
                      }
                              
                  storageCompanyRef.downloadURL { (url, error) in
                  if let metadateImage = url?.absoluteString {
                  dict[&quot;Company Image&quot;] = metadateImage
                     
                  companyRef.updateChildValues(dict, withCompletionBlock:  {
                  (error, ref) in
                  if error == nil {
                  print(&quot;Done&quot;)
                  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 ?? &quot;&quot;)
      return
                         }
      
  self.navigationController?.popViewController(animated: true)
      }
  }
  
  
  func createDatePickerForStart() {
      // center text in field
      startDateTextFieldConsiderations.textAlignment = .center
      
      // toolbar
      let toolbar = UIToolbar()
      toolbar.sizeToFit()
      
      // barbutton
      let doneButton = UIBarButtonItem(barButtonSystemItem: .done, target: nil, action: #selector(donePressedStart))
      toolbar.setItems([doneButton], animated: true)
      
      // assign toolbar to textfield
      
      startDateTextFieldConsiderations.inputAccessoryView = toolbar
      
      // assign datePicker to text field
      startDateTextFieldConsiderations.inputView = datePickerS
      
      // date picker mode
      datePickerS.datePickerMode = .date
      
  }
  
  func createDatePickerForEnd() {
      // center text in field
      endDateTextFieldConsiderations.textAlignment = .center
      
      // toolbar
      let toolbar = UIToolbar()
      toolbar.sizeToFit()
      
      // barbutton
      let doneButton = UIBarButtonItem(barButtonSystemItem: .done, target: nil, action: #selector(donePressedEnd))
      toolbar.setItems([doneButton], animated: true)
      
      // assign toolbar to textfield
      endDateTextFieldConsiderations.inputAccessoryView = toolbar
      
      // assign datePicker to text field
      endDateTextFieldConsiderations.inputView = datePickerE
      
      // date picker mode
      datePickerE.datePickerMode = .dateAndTime
  }
  
  @objc func donePressedStart() {
      // formatter
      let formatter = DateFormatter()
      formatter.dateStyle = .medium
      formatter.timeStyle = .none
      
      startDateTextFieldConsiderations.text = formatter.string(from: datePickerS.date)
      self.view.endEditing(true)
  }
  
  @objc func donePressedEnd() {
      // formatter
      let formatter = DateFormatter()
      formatter.dateStyle = .medium
      formatter.timeStyle = .medium
      
      endDateTextFieldConsiderations.text = formatter.string(from: datePickerE.date)
      self.view.endEditing(true)
     }
  }
      
  extension ConsiderationsTestViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
                 func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
                     //print(&quot;did Finish Picking Media&quot;)
                     if let image = info[UIImagePickerController.InfoKey(rawValue: &quot;UIImagePickerControllerEditedImage&quot;)] as? UIImage{
                         selectedImage = image
                         CompanyImage.image = image
                     }
                     dismiss(animated: true, completion: nil)
                 }
      }
  
  extension ConsiderationsTestViewController: UITableViewDelegate, UITableViewDataSource {
  func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -&gt; Int {
      return ConsiderationsTestViewController.numberOfPeople.count
  }
  
  func numberOfSections(in tableView: UITableView) -&gt; Int {
      return 1
  }
  
  
  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -&gt; UITableViewCell {
  
  let cell = tableView.dequeueReusableCell(withIdentifier: AddPersonCell, for: indexPath) as! ConsiderationsCell
      
      return cell
    }
  
  }
  </code></pre>
  <p>Here is the code for the view controller where I select the user and input their respective information:</p>
  <pre><code>import UIKit
  import Firebase
  
  class DropDownCellViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate, UITextFieldDelegate {
  
  var numberOfPeople = [String]()
  
  var users = [User]()
  
  var userName = [String]()
  
  var filteredNames: [String]!
  
  let dropDownCell = &quot;dropDownCell&quot;
  
  var emptyField = [String]()
  
  @IBOutlet weak var nameTextField: NoCopyPasteUITextField!
  
  @IBOutlet weak var tableView: UITableView!
  
  @IBOutlet weak var gridTextField: UITextField!
  
  @IBOutlet weak var storyTextField: UITextField!
  
  @IBOutlet weak var compensationTextField: UITextField!
  
  @IBAction func textFieldChanged(_ sender: AnyObject) {
      tableView.isHidden = true
  }
  
  @IBAction func cancelButtonTapped(_ sender: Any) {
      self.navigationController?.popViewController(animated: true)
  }
  
  @IBAction func addButtonTapped(_ sender: Any) {
      
      ConsiderationsTestViewController.numberOfPeople.append(&quot;\(nameTextField.text!)&quot;)
      
      self.navigationController?.popViewController(animated: true)
      
      print(ConsiderationsTestViewController.numberOfPeople)
  }
  
  let searchController = UISearchController(searchResultsController: nil)
  
  override func viewDidLoad() {
      super.viewDidLoad()
      
      updateDataArray()
      
      tableView.register(UserCell.self, forCellReuseIdentifier: dropDownCell)
      
      filteredNames = userName
      
          tableView.delegate = self
          tableView.dataSource = self
          nameTextField.delegate = self
             
          tableView.isHidden = true
             
             // Manage tableView visibility via TouchDown in textField
      nameTextField.addTarget(self, action: #selector(textFieldActive), for: UIControl.Event.touchDown)
         }
  
   override func didReceiveMemoryWarning() {
      super.didReceiveMemoryWarning()
   }
  
   override func touchesBegan(_ touches: Set&lt;UITouch&gt;, with event: UIEvent?)
    {
        guard let touch:UITouch = touches.first else
        {
            return;
        }
        if touch.view != tableView
        {
            nameTextField.endEditing(true)
            tableView.isHidden = true
        }
    }
  
   @objc func textFieldActive() {
         tableView.isHidden = !tableView.isHidden
     }
  
    // MARK: UITextFieldDelegate
     func textFieldDidEndEditing(_ textField: UITextField) {
         // TODO: Your app can do something when textField finishes editing
         print(&quot;The textField ended editing. Do something based on app requirements.&quot;)
     }
     
     func textFieldShouldReturn(_ textField: UITextField) -&gt; Bool {
         textField.resignFirstResponder()
         return true
     }
  
  
     func numberOfSections(in tableView: UITableView) -&gt; Int {
      // #warning Incomplete implementation, return the number of sections
      return 1
  }
  
     func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -&gt; Int {
      // #warning Incomplete implementation, return the number of rows
      
      return filteredNames.count
  }
  
     func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -&gt; UITableViewCell {
      
      let cell = tableView.dequeueReusableCell(withIdentifier: dropDownCell, for: indexPath) as!
      UserCell
      
      let user = users[indexPath.row]
      cell.textLabel?.text = user.name
      
      if let profileImageUrl = user.profileImageUrl {
      
      cell.profileImageView.loadImageUsingCacheWithUrlString(urlString: profileImageUrl)
          
      }
      return cell
  }
  
      func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -&gt; CGFloat {
  return 72
         }
  
      func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
      // Row selected, so set textField to relevant value, hide tableView
             // endEditing can trigger some other action according to requirements
             nameTextField.text = userName[indexPath.row]
             tableView.isHidden = true
             nameTextField.endEditing(true)
         }
  
      //Mark: Search Bar Config
  func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
      
      filteredNames = []
      
      if searchText == &quot;&quot; {
          filteredNames = userName
          
       }
      else {
          for names in userName {
              
              if names.lowercased().contains(searchText.lowercased()) {
                  
                  filteredNames.append(names)
              }
          }
      }
      self.tableView.reloadData()
       }
  
      func updateDataArray() {
      
      Database.database().reference().child(&quot;users&quot;).observe(.childAdded, with: { (snapshot) in
          
          if let dictionary = snapshot.value as? [String: AnyObject] {
              
              let user = User()
              //self.setValuesForKeys(dictionary)
              user.name = dictionary[&quot;name&quot;] as? String
              user.email = dictionary[&quot;email&quot;] as? String
              user.facebookUrl = dictionary[&quot;facebookUrl&quot;] as? String
              user.instagramUrl = dictionary[&quot;instagramUrl&quot;] as? String
              user.linkedInUrl = dictionary[&quot;linkedInUrl&quot;] as? String
              user.profileImageUrl = dictionary[&quot;profileImageUrl&quot;] as? String
              user.twitterUrl = dictionary[&quot;twitterUrl&quot;] as? String
              user.id = dictionary[&quot;id&quot;] as? String
              user.userType = dictionary[&quot;userType&quot;] as? String
              
              self.users.append(user)
              self.userName.append(user.name!)
              self.filteredNames.append(user.name!)
              
              DispatchQueue.main.async {
              self.tableView.reloadData()
              }
          }
          
         }, withCancel: nil)
      
      
      }
     }
  </code></pre>
  <p>I had a button in the original view controller that would print the array. I tested it before attempting to append the array. It was empty. I then added to people and tested it. It printed both the names but the table didn't change. I tried to dispatch.resync in the add button but it's too ambiguous. I really could use the help. Thanks! And thank you for the patience. I am new to coding and have no education in coding besides being self taught.</p>