2015-11-04 39 views
0

我的问题看起来很简单,但不起作用。我搜索了很多并尝试了很多方法,但它不想工作!如何更新,而不是重新加载UITableView?

所以,我有一个UITableView。在后台,我的一个功能可以工作并检测,如果接收到新消息,它会将此值写入Core Data,并且我希望在收到新消息时在我的单元格上添加徽章。

但当我尝试这个办法:

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

它不更新我的tableView并没有显示出新的徽章。对于这一点,我需要:

  1. 更改我的控制器,并返回到
  2. 或者我需要拖我的tableView顶部,让它

在这些情况下,它会告诉我的徽章。

那么,我怎样才能更新我的tableView显示新增加的徽章没有描述上述2种方法?

UPDATE

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消息,但它不更新它并且我的徽章

+0

你是如何创建你的表视图?你在哪里实现了它的数据源和委托? – Simon

+0

@Simon'class ContactsTableViewController:UITableViewController {'我添加了一个截图。请在我的问题中看看它 –

+0

尝试检查reloadTableView()是否从主线程调用。 –

回答

1

Objective-C中的示例:

[_tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationFade]; 

实例雨燕2.0:

self.tableView.reloadRowsAtIndexPaths([NSIndexSet, indexSetWithIndex:0], withRowAnimation: UITableViewRowAnimation.Automatic) 
0

你尝试NSOperationQeue?

func reloadTableView() { 
    NSOperationQueue.mainQueue().addOperationWithBlock { 
     self.tableView.reloadData() 
    }) 
}   

编辑如果你正在使用这样调用更新功能每秒的东西(因为我不能评论)

override func viewDidLoad() { NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: "reloadTableView", userInfo: nil, repeats: true) } 

,这是没有意义的。越来越之后,你可以把它叫做数据

2

尝试更新只接收新邮件与细胞:

self.tableView.beginUpdates() 
self.tableView.reloadRowsAtIndexPaths([NSIndexPath(forRow: row, inSection: section)], withRowAnimation: UITableViewRowAnimation.Fade) 
self.tableView.endUpdates() 

其中row - 你的细胞在这节收到消息的行

0

的reloadData应该可以工作......(我认为最好在主线程中调用它。)

你是否检查过你的对象数组是否正确?如果它来自CoreData请求,它可能会使用缓存。确保您已经同步CoreDataContext(通过在插入之后在上下文中调用save方法),并且您的提取返回最后创建的对象。

只是猜测...