Moving UITableViewCell between Sections in Grouped UITableView

fig.1. UITableView with grouped sections, but section 2 has no rows

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

fig.2. Section Footers are added

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

fig.3. The appearance after the move is complete.

Figure 3 shows the result of the move.

5 thoughts on “Moving UITableViewCell between Sections in Grouped UITableView

  1. 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 @” “;

  2. 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 ?

Leave a Reply

Your email address will not be published. Required fields are marked *