1

I want to enable drag and drop in tableview. I only want to drag items in app and I don't want it to be transferred to other app.


I found two ways to implement tableview rearrangement.

First way: use drag and drop delegate

drag delegate documentation supporting drag and drop in tableview in iOS11

It is realy important for me not to allow drag and drop between apps.

Is using drag delegate ok in this situation? Or should I use another solution?

// didLoad

[self.myTableView setDragDelegate: self];
[self.myTableView setDragInteractionEnabled: YES];

// itemsForBeginningDragSession

(NSArray<UIDragItem *> *)tableView:(UITableView *)tableView
                itemsForBeginningDragSession:(id<UIDragSession>)session
                                 atIndexPath:(NSIndexPath *)indexPath  API_AVAILABLE(ios(11.0)) {
Model *model = self.listOfX[indexPath.section];

NSItemProvider *itemProvider = [[NSItemProvider alloc] initWithObject: model];

UIDragItem *dragItem = [[UIDragItem alloc] initWithItemProvider: itemProvider];

return @[dragItem];
}

second way: tableview edit mode

[self.myTableView setEditing: YES];

I don't want to add a button and change the table view state to editing mode. I want to enable rearrangement by long press.

Fattaneh Talebi
  • 727
  • 1
  • 16
  • 42
  • Possible duplicate of [Swift - Drag And Drop TableViewCell with Long Gesture Recognizer](https://stackoverflow.com/questions/27631128/swift-drag-and-drop-tableviewcell-with-long-gesture-recognizer) – fishinear Apr 20 '19 at 13:53
  • @fishinear drag and drop delegate are new features in iOS 11, I want to find the best way to to drag and drop. with this new feature or with previous ones. – Fattaneh Talebi Apr 20 '19 at 13:59

2 Answers2

2

Your first option to use the drag and drop delegate is fine. Be sure you implement the UITableViewDragDelegate method - (BOOL)tableView:(UITableView *)tableView dragSessionIsRestrictedToDraggingApplication:(id<UIDragSession>)session and return YES. This will ensure the drag and drop must occur within the same app.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
0

If have been using FMMoveTable by Florian Mielke for this purpose, which works great. But he has now discontinued it, stating that you should use IOS 11 Drag & Drop functionality instead. Although I cannot see how you can restrict that to a single table.

Another possibility is to use the tutorial mentioned in this other StackOverflow answer.

fishinear
  • 6,101
  • 3
  • 36
  • 84