ios - 如何更新而不是重新加载 UITableView?

标签 ios iphone swift uitableview

我的问题可能看起来很简单,但行不通。我搜索了很多,尝试了很多方法,但它不想工作!

所以,我有一个 UITableView。在后台,我的一个函数工作并检测,如果收到新消息,它会将此值写入核心数据,我想在收到新消息时在我的单元格上添加角标(Badge)。

但是当我尝试这样做时:

func reloadTableView() {
    dispatch_async(dispatch_get_main_queue(),{
        self.tableView.reloadData()
    })
}

它不会更新我的 tableView,也不会显示新角标(Badge)。为此,我需要:

  1. 更改我的 Controller 并返回它
  2. 或者我需要将我的 tableView 拖到顶部并离开它

在这些情况下,它会显示我的角标(Badge)。

那么,如果不使用上述两种方法,我该如何更新我的 tableView 以显示新添加的角标(Badge)?

更新

enter image description here

更新 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/

相关文章:

iphone - 在 UIViewController 中更改 View

ios - Objective C 半只读属性

ios - 在 Collection View 中为 UImageView 添加标题标签

ios - 如何从sqlite中获取数据到数组中

iOS : UICollectionView inside UIScrollView index not incremented

ios - UITableView 的自定义部分标题 View 出现在模拟器上,但不出现在 iPhone 上。

iphone - 设置动画总是出现

iphone - 将数据从传感器传输到 iPhone

iOS 在 "detail view"而不是导航 Controller 创建的 RootVIewController 处启动应用程序

ios - 将固定大小设置为图像