私のチャットアプリでは、 "lastMessage"はエンティティFriendとメッセージの関係です。各Friendエンティティには一連のメッセージとlastMessageが1つしかありません。したがって、請求書にメッセージを送信すると、請求書の「lastMessage」が新しいタイムスタンプに更新されます。つまり、fetchedResultsControllerのフェッチされたオブジェクトには、各友人のlastMessage.timestampの配列が含まれています。私が持っている問題は、UITableViewです。チャットコントロールのメッセージに友人にメッセージを送り、DidChangeContent内の "messageTable.reloadData"を送信している間に、メッセージを送信した場合、彼のlastMessageは最新のタイムスタンプに更新され、fetchedResultsControllerの最初のエントリにソートされ、テーブルビューの最初の場所で彼を動かす。ただし、didChangeObjectの内部にself.messagesTable.reloadRowsAtIndexPaths([indexPath!]、withRowAnimation:UITableViewRowAnimation.None)を使用してアニメーションを追加する場合は、何も正しく再ロードされません。私はそれを入れてみました。挿入、更新、そして一度に。私はかなり新しくNSFetchedResultsControllerをtableViewに同期させています。だから、NSFetchedResultsControllerを使ってどのようにテーブルをリロードするのが普通であるのかよく分かりません。NSFetchedResultsControllerとUITableViewを同期させる
@IBOutlet weak var messagesTable: UITableView!
lazy var fetchedResultsController: NSFetchedResultsController = {
let fetchRequest = NSFetchRequest(entityName: "Friend")
fetchRequest.sortDescriptors = [NSSortDescriptor(key: "lastMessage.timestamp", ascending: false)]
let predicate = NSPredicate(format: "lastMessage.timestamp != nil")
fetchRequest.predicate = predicate
let context = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext
let frc = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: context, sectionNameKeyPath: nil, cacheName: nil)
frc.delegate = self
return frc
}()
func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath?, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath?) {
if type == .Insert {
self.messagesTable.reloadRowsAtIndexPaths([newIndexPath!], withRowAnimation: UITableViewRowAnimation.None)//reloadItemsAtIndexPaths([indexPath!])
self.messagesTable.reloadRowsAtIndexPaths([indexPath!], withRowAnimation: UITableViewRowAnimation.None)//reloadItemsAtIndexPaths([indexPath!])
}
if type == .Update{
// self.collectionView?.cha([newIndexPath!])
print("h")
self.messagesTable.reloadRowsAtIndexPaths([newIndexPath!], withRowAnimation: UITableViewRowAnimation.None)//reloadItemsAtIndexPaths([indexPath!])
self.messagesTable.reloadRowsAtIndexPaths([indexPath!], withRowAnimation: UITableViewRowAnimation.None)//reloadItemsAtIndexPaths([indexPath!])
}
if type == .Move{
// self.collectionView?.cha([newIndexPath!])
print("h")
self.messagesTable.reloadRowsAtIndexPaths([newIndexPath!], withRowAnimation: UITableViewRowAnimation.None)//reloadItemsAtIndexPaths([indexPath!])
self.messagesTable.reloadRowsAtIndexPaths([indexPath!], withRowAnimation: UITableViewRowAnimation.None)//reloadItemsAtIndexPaths([indexPath!])
}
}
func controllerDidChangeContent(controller: NSFetchedResultsController) {
self.messagesTable.beginUpdates()
}