2016-03-01 61 views
3

我有tableViewNSFetchedResultsController偵聽項更新。NSFetchedResultsController無法在表更新時滾動

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

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

     switch type { 
      case .Insert: 
       if let _newIndexPath = newIndexPath { 
        self.tableView.insertRowsAtIndexPaths([_newIndexPath], withRowAnimation: .None) 
       } 
      case .Delete: 
       if let _indexPath = indexPath { 
        self.tableView.deleteRowsAtIndexPaths([_indexPath], withRowAnimation: .None) 
       } 
      case .Update: 
       if let _indexPath = indexPath { 
        self.tableView.reloadRowsAtIndexPaths([_indexPath], withRowAnimation: .None) 
       } 
      default: 
       self.tableView.reloadData() 
     } 

     self.items = controller.fetchedObjects as! [Item] 
    } 

func controllerDidChangeContent(controller: NSFetchedResultsController) { 
     self.tableView.endUpdates() 
    } 

當用戶添加的項目我在每1-2秒數據庫更新它,我不能向下滾動,因爲控制器刷新項目,滾動我到頂部。

+1

你也有'controllerWillChangeContent'和'controllerDidChangeContent'實施? –

+0

是的,我更新了答案 – Arti

+0

所以你不能向下滾動,因爲你的tableView正在重新加載,並帶你回到頂部? –

回答