TableView在Swift和Xcode中的Modal Segue之后重新加载数据

时间:2019-06-11 18:45:55

标签: swift xcode uitableview uiviewcontroller segue

我正在尝试将消息传递功能构建到我正在开发的新应用程序中。这工作得很好,但是当我使用Modal Segue从Messaging View Controller(一个发送和接收消息的控制器)切换到Messages View Controller(显示要向其发送消息的用户的控制器)时,TableView将重新加载。这会导致片刻之后TableView为空白的短暂片刻。起初,我认为这可能是由于ViewDidLoad中调用了我的函数,该函数要求重新加载TableView,但是删除它之后,仍然重新加载TableView。我不确定我可以从这里尝试什么。我事先为嵌套表示歉意。稍后我将对其进行清理,但现在可以使用。有什么帮助吗?下面的代码:

override func viewDidLoad() {
    super.viewDidLoad()

    getEmployers()
}

func getEmployers() {

    print("ONE")
    let uid = Auth.auth().currentUser?.uid

    Database.database().reference().child("users").child(uid!).child("Upcoming Jobs").observeSingleEvent(of: .value, with: { (snapshot) in
        if let dictionary = snapshot.value as? [String: AnyObject] {
            print("TWO")
            let array = Array(dictionary.keys)
            var numInArray = 0
            for item in array {
                Database.database().reference().child("users").child(uid!).child("Upcoming Jobs").child(item).observeSingleEvent(of: .value, with: { (snapshot) in
                    if let dictionary = snapshot.value as? [String: AnyObject] {
                        print("THREE")
                        let euid = dictionary["Employer"] as? String
                        numInArray += 1
                        if !self.employerUIDs.contains(euid!) {
                            self.employerUIDs.append(euid!)
                            Database.database().reference().child("users").child(euid!).child("Messages").child(uid!).observeSingleEvent(of: .value, with: { (snapshot) in
                                if let dictionary = snapshot.value as? [String: AnyObject] {
                                    let array = Array(dictionary.keys)
                                    var array2 = [Int]()
                                    for item in array {
                                        var item2 = item
                                        item2 = item2.replacingOccurrences(of: "-", with: "", options: .literal, range: nil)
                                        item2 = item2.replacingOccurrences(of: ":", with: "", options: .literal, range: nil)
                                        item2 = item2.replacingOccurrences(of: " ", with: "", options: .literal, range: nil)
                                        print(item2)
                                        array2.append(Int(item2)!)
                                    }
                                    array2.sort(by: >)
                                    print("SUBSTRING:")
                                    var timeStamp = String(array2[0])
                                    timeStamp = self.insertChar(char: "-", location: 4, string: timeStamp)
                                    timeStamp = self.insertChar(char: "-", location: 7, string: timeStamp)
                                    timeStamp = self.insertChar(char: " ", location: 10, string: timeStamp)
                                    timeStamp = self.insertChar(char: ":", location: 13, string: timeStamp)
                                    timeStamp = self.insertChar(char: ":", location: 16, string: timeStamp)
                                Database.database().reference().child("users").child(euid!).child("Messages").child(uid!).child(timeStamp).observeSingleEvent(of: .value, with: { (snapshot) in
                                        if let dictionary = snapshot.value as? [String: AnyObject] {
                                            let recentMessage = dictionary["Message"] as? String
                                            self.createUser(uid: euid!, countOfArray: array.count, num: numInArray, message: recentMessage!, time: array2[0])
                                        }
                                    })
                                }
                                else {
                                    //print("WE ARE NOT YET MESSAGING" )
                                }
                            })
                        }
                    }
                })
            }
        }
    })
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return numEmployers
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell2") as? EmployerTableViewCell
    tableView.rowHeight = 80
    if indexPath.row < employers.count {
        cell!.setInfo(fn: employers[indexPath.row].firstName, ln: employers[indexPath.row].lastName, lastMssg: employers[indexPath.row].lastMessage, lastDate: employers[indexPath.row].dateOfLastMessage)
    }
    return cell!
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "MessagesToMessaging" {
        let val = tableView?.indexPathForSelectedRow
        uidOfOtherUser = employers[val![1]].uidOfEmployer
        tableView.deselectRow(at: tableView.indexPathForSelectedRow!, animated: true)
    }
}

0 个答案:

没有答案