开发者

Drag and drop between two tables in ipad

开发者 https://www.devze.com 2023-01-19 12:08 出处:网络
I have two UITableView in my iPad application. I want to drag a cell from one tableviewand drop onto another tableview.

I have two UITableView in my iPad application. I want to drag a cell from one tableview and drop onto another tableview. Please suggest me any idea how can I Implement drag & drop between t开发者_如何学编程wo tables in iPad ?

Thanks in advance


I've implemented a solution to this before.

Approach

  • The main component of the solution is class that listens for drag / drop events and broadcasts them to a delegate; I called this component the gesture coordinator. It handles the events emitted from a UIGestureRecognizer to calculate the 'drag and drop' state and notify the delegate.
  • For example, a view controller acting as its delegate would receive messages about when items have been exchanged between collections and then update its collection views and data sources.
  • The gesture coordinator is essentially just a drag-and-drop decorator for a UIGestureRecognizer.

Gesture Coordinator Logic

Here are the propositions that I considered when implementing the gesture coordinator:

  • A collection is a view that contains and array of child items.
  • A drag arena consists of a superview and an ordered set of collections that exist as subviews within that superview.
  • The order of the collections in the drag arena determines their drag / drop priority. That is, if a collection sits at the beginning of the drag arena's ordered set of collections, then drags and drops occurring on that collection will be recognized in place of any of the later collections in the set.
  • A drag starts if and only if a gesture is started within the bounds of a draggable item of a collection in the drag arena. Dragging occurs if and only if, immediately after a drag has been started, the location of the gesture changes within the drag arena.
  • A drag stops if and only if immediately after dragging the gesture stops, is cancelled or finishes.
  • A deletion occurs if and only if the drag stops at a point which is specified as being deletable. For example, the user may designate certain bounds within the drag arena to be 'delete on drop' areas.
  • A rearrange occurs if and only if the drag stops within the bounds of the collection that it started in, on a different item in that collection which is specified as being rearrangeable, and on a point in the drag arena that is not specified as being deletable.
  • A drop occurs if and only if the drag stops within the bounds of another collection in the drag arena, on a specific item or point that is specified as droppable within that collection, and on a point in the drag arena which is not specified as being deletable.

References

  • Documentation
  • Use Case examples


Once you touch upon a row in one table.. You must create a image of the row you want to drag

This could be done by editing didSelectRowAtIndexPath function from where you can actually determine which row was selected.

then move that image along with your drag (by custom implementation of touches:Moved)

CGPoint gesturepoint = [touch locationInView:self.view];

imageview.centre = gesturepoint;


Now you can do this help with dragDelegate and dropDelegate of UITableView from iOS11


Several steps:

  1. Add long press gesture recoginzer, when it's detected, get UIImage from cell you press.
  2. Remove the cell from the tableview (deleteRowsAtIndexPaths:withRowAnimation:). Created a UIImageView by the UIImage you get from step 1. Move the UIImageView while you move the finger.
  3. When the end of gesture, decide the tableview to drop and the location to insert. Then add the selected data to the data source (insertRowsAtIndexPaths:withRowAnimation:).
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号