Asif Khan
Asif Khan

Reputation: 1

Re-Order UITableView Sections only

I have been trying to find some way to re-oder table sections by drag and drop exactly like re-ordering table rows but no success. can someone guide me where to go? I only need to re order complete section not some rows.

Upvotes: 0

Views: 269

Answers (1)

vadian
vadian

Reputation: 285260

Simple way with the standard delegate methods

  • Implement tableView:moveRowAtIndexPath:toIndexPath:
    data represents the data source array containing the sections.

    func tableView(tableView: UITableView, moveRowAtIndexPath sourceIndexPath: NSIndexPath, toIndexPath destinationIndexPath: NSIndexPath) {
       let itemToMove = data[sourceIndexPath.section]
       data.removeAtIndex(sourceIndexPath.section)
       data.insert(itemToMove, atIndex: destinationIndexPath.section)
       tableView.reloadData()
     }
    
  • Set editing of the table view to true. The reorder symbol appears.

  • Drag a row. The code in method moveRowAtIndexPath reorders the entire section.

Upvotes: 2

Related Questions