我的问题可能看起来很简单,但行不通。我搜索了很多,尝试了很多方法,但它不想工作!
所以,我有一个 UITableView。在后台,我的一个函数工作并检测,如果收到新消息,它会将此值写入核心数据,我想在收到新消息时在我的单元格上添加角标(Badge)。
但是当我尝试这样做时:
func reloadTableView() {
dispatch_async(dispatch_get_main_queue(),{
self.tableView.reloadData()
})
}
它不会更新我的 tableView,也不会显示新角标(Badge)。为此,我需要:
- 更改我的 Controller 并返回它
- 或者我需要将我的 tableView 拖到顶部并离开它
在这些情况下,它会显示我的角标(Badge)。
那么,如果不使用上述两种方法,我该如何更新我的 tableView 以显示新添加的角标(Badge)?
更新
更新 2
我有 2 个文件:ContactsTableViewController 和 Messages.swift。
在我的 Messages.swift 中,我处理新消息,当我收到新消息时,我会在日志中获取:
print("New message for \(user.jidStr)")
ContactsTableViewController().reloadTableView()
在我的 ContactsTableViewController 中:
func reloadTableView() {
self.tableView.reloadData()
print("updated")
}
它重新加载我的 tableView,因为我在日志中收到 updated
消息,但它没有更新它和我的角标(Badge)
最佳答案
尝试只更新接收新消息的单元格:
self.tableView.beginUpdates()
self.tableView.reloadRowsAtIndexPaths([NSIndexPath(forRow: row, inSection: section)], withRowAnimation: UITableViewRowAnimation.Fade)
self.tableView.endUpdates()
其中 row
- 接收消息的单元格所在的行
关于ios - 如何更新而不是重新加载 UITableView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33516555/