0

I have a table view hooked up to a fetchedResultsController. When the table goes into editing mode I can get the delete to happen but the animation is strange. The red delete button instead of fading up with the rest of the row stays at full height and moves right. Also when swiping to delete the row, red bar stays at full row height for its animation.

The delete operation is surrounded by a beginTableUpdates : endTableUpdates block.

A video of this is at https://youtu.be/YOxex0sROwU

I'm using MVVM (or attempting to!)

I have some closures defined

var willChangeContent: (() -> ())?
var didChangeContent: (() -> ())?
var didInsertRow: ((_ indexPath: IndexPath) -> ())?
var didDeleteRow: ((_ indexPath: IndexPath) -> ())?
var didUpdateRow: ((_ IndexPath: IndexPath) -> ())?
var didMoveRow: (( _ IndexPath: IndexPath, _ newIndexPath: IndexPath) -> ())?

And the fetchedResultsController in an extension to the viewModel

extension TrainingEventsTableViewViewModel: NSFetchedResultsControllerDelegate {

func controllerWillChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {
    willChangeContent?()
}

func controllerDidChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {
    didChangeContent?()
}

func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChange anObject: Any, at indexPath: IndexPath?, for type: NSFetchedResultsChangeType, newIndexPath: IndexPath?) {
    switch type {
    case .insert:
        didInsertRow?(newIndexPath!)
    case .delete:
        didDeleteRow?(indexPath!)
    case .update:
        didUpdateRow?(indexPath!)
    case .move:
        didMoveRow?(indexPath!, newIndexPath!)
    }
  }
}

and in the uiTableViewController the closures are:

 private func bindToModel() {

    viewModel?.willChangeContent = { [unowned self] () in
        self.tableView.beginUpdates()
    }

    viewModel?.didChangeContent = { [unowned self] () in
        self.tableView.endUpdates()
    }

    viewModel?.didInsertRow = { [unowned self] (indexPath) in
        self.tableView.insertRows(at: [indexPath], with: .automatic)
    }

    viewModel?.didDeleteRow = { [unowned self] (indexPath) in
        self.tableView.deleteRows(at: [indexPath], with: .automatic)
    }

    viewModel?.didUpdateRow = { [unowned self] (indexPath) in
        self.tableView.reloadRows(at: [indexPath], with: .automatic)
    }

    viewModel?.didMoveRow = { [unowned self] (indexPath, newIndexPath) in
        self.tableView.deleteRows(at: [indexPath], with: .automatic)
        self.tableView.insertRows(at: [newIndexPath], with: .automatic)
    }

}

The only other different thing is that the cell is coming from a xib file rather than a prototype cell on the storyboard.

Mark J
  • 3
  • 4

0 Answers0