Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSFetchedResultsController can't scroll while table updating

I have a tableView with NSFetchedResultsController that listen for item updates.

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()
    }

When user add item i update it in database every 1-2 seconds and i can't scroll down, because controller refresh item and scroll me to top.

like image 219
Arti Avatar asked Mar 17 '26 08:03

Arti


1 Answers

I found problem. It is because i use:

self.tableView.rowHeight = UITableViewAutomaticDimension

Thanks for this answer: UITableView bouncing back to the top of a section when calling reloadRowsAtIndexPaths

like image 164
Arti Avatar answered Mar 20 '26 06:03

Arti



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!