UITableView contains the necessary logic to allow movement of cells (or rows) between sections in a grouped UITableView, but there appears to be a bug that prevents you from moving a row into an empty section. There is however a workaround!
The Problem
Figure 1 shows how the view might appear. Section 1 has two rows, and section 2 has no rows. If you put the view into editing mode (setEditing:animated:) so that the editing controls appear, and try to drag either row 1 or 2 down to section 2, you’ll see that you can’t drag it far enough to make it stick in section 2.
Don’t forget that you need to implement tableView:canMoveRowAtIndexPath: and tableView:moveRowAtIndexPath: in order for the reordering controls to appear in the view.
The Solution
The trick is to add some content to the footer for each section (or at least the empty sections), as this then creates a void into which the dragged cell can be placed. Figure 2 shows how this would look with editing enabled. Note that the footers only need to appear when in editing mode, if not already defined.
To add the section footers, implement tableView:titleForFooterInSection:, such as:
-(NSString *) tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section { if (self.tableView.isEditing) { return @"End of Group"; } return nil; }
The Result
Figure 3 shows the result of the move.
Thank you, thank you, thank you 🙂 I first tried with a dummy cell for row 0, but yours is the better workaround for sure!
By the way: It even works with return @” “;
Hello and thx to share this solution. 🙂
Besides this I’m trying to get a Tableview grouped style working with reordoring rows on several sections.
Is yours fully works ?
I was only experimenting with the controls, so didn’t build a complete solution.
Thank you for your post. Can you send me your project to email “duc@bizzon.com.vn”.
Sorry, I don’t have the test project any more.