Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reorder sections of a UITableView?

This question from 2009 wasn't answered, and it is one of the very few mentions of this problem on the Internet.

The problem: I want to re-order the sections of a UITableView in the same way I can do with the rows (i.e., by dragging).

The way I see this working like is: you turn on editing on the table, re-order indicators appear. When you start dragging a section header (or a cell acting as a fake section header, which is sub-optimal [1]), all the regular cells "disappear", so you can only move sections around (and it is easier if you have more than a handful of sections).

LPRTableView appears to address the problem, but it is quite tricky to set up correctly when re-ordering sections. I tried for a full day with not very much success. Plus, [1].

A not-so-great-but-acceptable solution is to have buttons like ⬆️⬇️ in the headers (proper headers can be used here) but it is a little awkward.

Another not-so-great-but-acceptable solution is to have an extra step (like, a button) that brings up a new UITableView showing only the sections as cells, and allow re-ordering there. Works fine, but creates more issues than it solves [2].

I can't see anything in the docs by way of, for example, a hook called when re-ordering starts/ends. Or rather, there is the ending bit but I can't see the beginning one.

Suggestions?


[1] This allows rows to be placed before the section header, in principle, and you have to manually catch that, with a bad-looking visual effect.

[2] Where would I even place such a control, especially if I have a crowded UI already, or I want to keep my UI minimal?

like image 370
Morpheu5 Avatar asked May 17 '18 13:05

Morpheu5


1 Answers

I would go the way to collapse all sections and display only the sectionHeaders as UITableViewCells. Then i would rearrange them with the default drag and drop behaviour. When finished, then load the old uncollapsed info back into the table view cell.

Then you don't have to think about animations and stick to the "iOS Guidelines".

like image 73
Christoph Lückler Avatar answered Oct 16 '22 23:10

Christoph Lückler