Reorder some rows but not all

I'm still writing iOS-apps using Swift and UIKit instead of SwiftUI — I've never been one of those cool kids using the latest tech, and my 100 days of SwiftUI lasted for 33 days or so. Maybe I should give it another chance. But today I found some nice things in UITableViewDelegate and UITableViewDataSource, you all probably already know, but those made my life easier, and so I'm writing this piece mostly for my future self.


The task was to build a form — not using SwiftUI, but UIKit. The user should be able to reorder some rows, but not all, and only in some sections, but not all. I recorded a short video from the Simulator to show the desired behaviour:


I built the form using a UITableView with the grouped style, as you might have already guessed. The tableView itself is in editing-mode, but I hide the delete-buttons invisible aka set the editing style to none.

The interesting part is to allow only certain cells to be moveable. I achieve this by using tableView(_:canMoveRowAt:). But that's only one part of the magic, as implementing only this method, I still can sneak rows into places where they don't belong to. And this is where tableView(_:targetIndexPathForMoveFromRowAt:toProposedIndexPath:) comes into play: By implementing this method I can control, where the movable rows can be moved to. So, task accomplished, Feierabend.

In 2013, someone at Apple wrote about this in the documentation archive. I read the full article just now, like six hours to late. Oh, of course there's a Github-repo with some sample-code.