撰写警报文本字段

问题描述 投票:0回答:0

我正在构建的应用程序是一个表格视图,您可以通过单击添加按钮将新人添加到表格视图。当您这样做时,会弹出一个警报操作,您可以在其中输入姓名、电话号码和地址。

姓名和电话号码需要存储在自定义表格视图单元格的顶部,地址位于表格视图单元格的底部。我在表格视图单元格上的任何地方都不需要 documentID。存储在 Firebase 中。

我怎样才能在顶部表格视图单元格上显示姓名和电话号码,在底部显示地址?

import UIKit
import FirebaseDatabase
import FirebaseCore
import Firebase
import Firestore

class TableViewController: UITableViewController {
    var db:Firestore!
    var employeeArray = [Employee]()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        db = Firestore.firestore()
        loadData()
        checkForUpdates()
    }
    
    func loadData() {
        db.collection("employee").getDocuments() {
            querySnapshot, error in
            if let error = error {
                print("\(error.localizedDescription)")
            }else{
                self.employeeArray = querySnapshot!.documents.compactMap({Employee(id: $0.documentID, xdictionary: $0.data())})
                DispatchQueue.main.async {
                    self.tableView.reloadData()
                }
            }
        }
    }
    
    func checkForUpdates() {
        db.collection("employee").whereField("timeStamp", isGreaterThan: Date())
            .addSnapshotListener {
                querySnapshot, error in
                guard let snapshots = querySnapshot else {return}
                
                snapshots.documentChanges.forEach {
                    diff in
                    
                    if diff.type == .added {
                        self.employeeArray = querySnapshot!.documents.compactMap({Employee(id: $0.documentID, xdictionary: $0.data())})
                        DispatchQueue.main.async {
                            self.tableView.reloadData()
                        }
                    }
                }
            }
    }
    
    @IBAction func addEmployee(_ sender: Any) {
        let composeAlert = UIAlertController(title: "Add Employee", message: "Add Employee", preferredStyle: .alert)
        
        composeAlert.addTextField { (textField:UITextField) in
            textField.placeholder = "Name"
        }
        
        composeAlert.addTextField { (textField:UITextField) in
            textField.placeholder = "Adress"
        }
        composeAlert.addTextField { (textField:UITextField) in
            textField.placeholder = "Phone Number"
            
            composeAlert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
            
            composeAlert.addAction(UIAlertAction(title: "Add Employee", style: .default, handler: { (action:UIAlertAction) in
                let firts = 0
                let second = 1
                let third = 2
                let fourth = 3
                if let name = composeAlert.textFields?[firts].text,
                let adress = composeAlert.textFields?[second].text,
                let phoneNumber = composeAlert.textFields?[third].text,
                let documentID = composeAlert.textFields?[fourth].text
                {
                    let newEmployee = Employee(name: name, adress: adress, phoneNumber: phoneNumber,documentID: documentID)
                    
                    var ref:DocumentReference? = nil
                    
                    ref = self.db.collection("employee").addDocument(data: newEmployee.dictionary) { [self]
                        error in
                        
                        if let error = error {
                            print("Error adding document: \(error.localizedDescription)")
                        }else{
                            loadData()
                            checkForUpdates()
                            print("Document added with ID: \(ref!.documentID)")
                        }
                    }
                }
                
            }))
        }
            self.present(composeAlert, animated: true, completion: nil)
        }
        
        
        // MARK: - Table view data source
        
        override func numberOfSections(in tableView: UITableView) -> Int {
            return 1
        }
        
        override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return employeeArray.count
        }
        
        override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
            
            let tweet1 = employeeArray[indexPath.row]
            cell.textLabel?.text = "\(tweet1.name) \(tweet1.phoneNumber)"
            cell.detailTextLabel?.text = "\(tweet1.adress)"
             
            return cell
        }
        
        func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) async {
        }

        override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
            return true
        }
        
        override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
            if (editingStyle == UITableViewCell.EditingStyle.delete) {
                db.collection("employee").document(employeeArray[indexPath.row].documentID).delete() { [self] err in
                    if let err = err {
                        print("Error removing document: \(err)")
                    } else {
                        loadData()
                        checkForUpdates()
                    }
                }
            }
        }
    
}
ios swift uitableview
© www.soinside.com 2019 - 2024. All rights reserved.