0

I am making a table view where you can drag and drop the cells to sort them in the right order. However, the current sorting uses the swap method. If I want to move cell 10 to cell 1, it swaps them. I don't want that. I want cell10 to move to cell1, but cell1 and the rest of them one cell downward as if we made an insertion. How can I do that? (items is the name of the array)

    func tableView(tableView: UITableView, moveRowAtIndexPath sourceIndexPath: NSIndexPath, toIndexPath destinationIndexPath: NSIndexPath) {
    if (sourceIndexPath.row != destinationIndexPath.row){
        swap (&items[sourceIndexPath.row], &items[destinationIndexPath.row])
    }
    tableView.reloadData()
}
Osaama Shehzad
  • 147
  • 1
  • 2
  • 12
  • items is nsarray or nsmutablearray? – Bhadresh Kathiriya Jun 27 '16 at 12:39
  • See the `.insert` part in the accepted answer on the linked page. – Eric Aya Jun 27 '16 at 12:41
  • it can be changed. – Osaama Shehzad Jun 27 '16 at 12:42
  • @EricD I can't find the linked page. Can you provide me the link? I am new to stackexchange – Osaama Shehzad Jun 27 '16 at 12:44
  • @OsaamaShehzad I was referring to the link in the yellow box at the top of this page, it was automatically inserted when I've closed your question as duplicate: "This question already has an answer here: [Add an element to an array in Swift](http://stackoverflow.com/questions/24002733/add-an-element-to-an-array-in-swift)". :) – Eric Aya Jun 27 '16 at 12:45
  • @EricD thank you so much! insertion part i understood but now how do i keep the size of array same because every time i insert, i must delete it from the previous index. – Osaama Shehzad Jun 27 '16 at 12:51
  • I would do the same: remove from index Y then insert at index X. // I've closed this question because it already had an answer, but if you feel like you have a new, different question, don't hesitate to post a new question. :) Just be sure to check if there isn't any already existing Question & Answer about the topic (and I bet there is :p) before posting again. – Eric Aya Jun 27 '16 at 12:54

0 Answers0