首页 > 解决方案 > TableView 在 Swift 和 Xcode 中的 Modal Segue 之后重新加载数据

问题描述

我正在尝试将消息传递功能构建到我一直在开发的新应用程序中。这工作得很好,但是当我使用 Modal segue 从消息视图控制器(发送和接收消息的那个)切换到消息视图控制器(显示要发送消息的用户的那个)时,TableView 重新加载. 这会导致 TableView 在 segue 之后短暂变为空白。起初,我认为这可能是由于我在 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)
    }
}

标签: swiftxcodeuitableviewuiviewcontrollersegue

解决方案


推荐阅读