2016-08-19 5 views
1

私のチャットアプリでは、 "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() 


    } 

答えて

1

代わりのreloadRowsAtIndexPathsinsertRowsAtIndexPathsdeleteRowsAtIndexPaths及び方法を使用。また、controllerWillChangeContentメソッドではbeginUpdatescontrollerDidChangeContentではendUpdatesを使用してください。例:

func controllerWillChangeContent(controller: NSFetchedResultsController) { 
    self.messagesTable.beginUpdates() 
} 

func controller(controller: NSFetchedResultsController, didChangeSection sectionInfo: NSFetchedResultsSectionInfo, atIndex sectionIndex: Int, forChangeType type: NSFetchedResultsChangeType) { 
    switch type { 
     case .Insert: 
      self.messagesTable.insertSections(NSIndexSet(index: sectionIndex), withRowAnimation: .Fade) 
     case .Delete: 
      self.messagesTable.deleteSections(NSIndexSet(index: sectionIndex), withRowAnimation: .Fade) 
     default: 
      return 
    } 
} 

func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath?, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath?) { 

    switch type { 
     case .Insert: 
      self.messagesTable.insertRowsAtIndexPaths([newIndexPath!], withRowAnimation: .Fade) 
     case .Delete: 
      self.messagesTable.deleteRowsAtIndexPaths([indexPath!], withRowAnimation: .Fade) 
     case .Update: 
      self.messagesTable.reloadRowsAtIndexPaths([indexPath!], withRowAnimation: .Fade) 
     case .Move: 
      self.messagesTable.deleteRowsAtIndexPaths([indexPath!], withRowAnimation: .Fade) 
      self.messagesTable.insertRowsAtIndexPaths([newIndexPath!], withRowAnimation: .Fade) 
    } 
} 

func controllerDidChangeContent(controller: NSFetchedResultsController) { 
    self.messagesTable.endUpdates() 
} 
関連する問題