在uitableview cell.detailTextLabel - Swift中存储与Firebase不同的日期

时间:2018-04-04 18:57:15

标签: swift uitableview firebase firebase-realtime-database

我有一个firebase数据库,用于导入用户体重测量值并将其显示在表格视图中。我希望我的firebase数据库中显示的相应日期显示在详细信息标签中。目前,每当我在不同的日子刷新表格时,整个表格都会刷新以匹配日期。我可以做些什么改变来解决这个问题。 (见下图)

import UIKit
import Firebase

class WeightViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, UITextFieldDelegate {

    // Declarations of Database and Added Exercise
    var addMes:[String] = []
    var handle: DatabaseHandle?
    var ref: DatabaseReference?
    var keyArray: [String] = []


    // Storyboard connections
    @IBOutlet weak var tableView: UITableView!
    @IBOutlet weak var mesView: UITextField!
    @IBAction func inputButton(_ sender: Any) {

        // Current user insert of exercise
        if mesView.text != "" {
            ref?.child("user").child(Auth.auth().currentUser!.uid).child("measurements").child("weight").child("\(getDate())").setValue(mesView.text)
            mesView.text = ""
            let alertController = UIAlertController(title: "Nice One!", message: "Your weight has been stored. If you want to amend, just enter a new weight and it will be overwritten for today. ", preferredStyle: .alert)
            let defaultAction = UIAlertAction(title: "OK", style: .cancel, handler: nil)
            alertController.addAction(defaultAction)
            present(alertController, animated: true, completion: nil)
        }
        else
            // Alert if nothing is entered
            if mesView.text == "" {
                let alertController = UIAlertController(title: "Oh dear...", message: "You can't submit nothing", preferredStyle: .alert)
                let defaultAction = UIAlertAction(title: "OK", style: .cancel, handler: nil)
                alertController.addAction(defaultAction)
                present(alertController, animated: true, completion: nil)
        }
    }
    // Table returns number of exercises
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return addMes.count
    }
    // Cell textLabel equals exercise entered
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = UITableViewCell(style: .value1, reuseIdentifier: "cell")
        cell.textLabel?.text = addMes[indexPath.row]
        cell.detailTextLabel?.text = getDate()
        return cell
    }

    // Do any additional setup after loading the view.
    override func viewDidLoad() {
        super.viewDidLoad()

        // Adds target to add measurement type
        mesView.delegate = self
        mesView.addTarget(self, action: #selector(self.textFieldDidChange), for: .editingChanged)

        tableView.allowsMultipleSelectionDuringEditing = true

        // Start of hide keyboard
        self.mesView.delegate = self

        // Database Reference
        ref = Database.database().reference()

        // Adds notes child
        handle = ref?.child("user").child(Auth.auth().currentUser!.uid).child("measurements").child("weight").observe(.childAdded, with: { (snapshot) in
            if let item = snapshot.value as? String
            {
                self.addMes.append(item)
                self.tableView.reloadData()
            }
        })

    }
    // Adds measurement
    @objc func textFieldDidChange(textfield: UITextField) {
        var text = mesView.text?.replacingOccurrences(of: "kg", with: "")
        text = text! + "kg"
        mesView.text = text

        print("Text changed")
    }
    // Allows editing of cell
    func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        return true
    }
    // Delete cell
    func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
        print(indexPath.row)
        if editingStyle == .delete {
            GetAllKeys()
            let when = DispatchTime.now() + 1
            DispatchQueue.main.asyncAfter(deadline: when, execute: {
                self.ref?.child("user").child(Auth.auth().currentUser!.uid).child("measurements").child("weight").child(self.keyArray[indexPath.row]).removeValue()

                self.addMes.remove(at: indexPath.row)
                tableView.deleteRows(at: [indexPath], with: .automatic)
                self.keyArray = []
            })
        }

    }
    // Retrieves all keys from Firebase
    func GetAllKeys() {
        ref?.child("user").child(Auth.auth().currentUser!.uid).child("measurements").child("weight").observeSingleEvent(of: .value, with: { (snapshot) in
            for child in snapshot.children {
                let snap = child as! DataSnapshot
                let key = snap.key
                self.keyArray.append(key)
            }
        })
    }
    // End of hide keyboard when user touches outside keyboard
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        self.view.endEditing(true)
    }
    //Presses return key
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        mesView.resignFirstResponder()
        return true
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
    {
        tableView.deselectRow(at: indexPath as IndexPath, animated: true)
    }
    func getDate() -> String {
        let date = Date()
        let calendar = Calendar.current
        // hours + min:  -\(calendar.component(.hour, from: date))-\(calendar.component(.minute, from: date))
        return "\(calendar.component(.year, from: date))-\(calendar.component(.month, from: date))-\(calendar.component(.day, from: date))"
    }
    // How button pressed
    @IBAction func How(_ sender: Any) { UIApplication.shared.open(URL(string: "https://www.wikihow.com/Take-Body-Measurements")! as URL, options: [:], completionHandler: nil)
    }
}

Firebase data tree

enter image description here

0 个答案:

没有答案