swift - 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)
}
}
解决方案
推荐阅读
- oracle - Oracle 12c 如何处理 DATE 数据类型的时区数据
- vhdl - 找到运算符“-”的“0”定义,无法确定“-”的精确重载匹配定义
- flutter - 通过使用 Provider 包或创建对象,哪个是公开对象的更好方法
- java - Java:多个 SSL 连接同时具有独立的 SSL 会话
- python - 如何根据 pandas 中的一组特定日期和 python 中的 datareader 获得一组价格
- python - 我需要在 python 中使用 lxml 获取元标记的值
- c++ - 素数查找算法返回奇怪的值
- python - pygame 不适用于 pycharm 社区版(无响应)
- sqlplus - 如何解决 SP2-0027:使用 Liquibase 生成的 ddl 脚本的 Input is too long 错误?
- windows - 操作无法完成。加载类型库/dll .Net 时出错