As the title says. Reordering within a single UITableView is trivial, but the screen of the iPad is large enough to display multiple UITableViews at the same time. So it seems like there should be a way to drag and drop a UITableViewCell between two UITableViews. Any thoughts on the best approach?
The way I achieve adding spacing between cells is to make numberOfSections = "Your array count" and make each section contains only one row. And then define headerView and its height. This works great.
TableView can be defined as the view which can arrange the data using rows in a single column. It is used in almost every iOS application, for example, contacts, facebook, Instagram, etc. The tableview is the instance of the UITableView class, which inherits the UIScrollView class.
Datasource methods are used to generate tableView cells,header and footer before they are displaying.. Delegate methods provide information about these cells, header and footer along with other user action handlers like cell selection and edit..
My solution was to use a custom UIGestureRecognizer
for tracking the touch events, and a separate view for drawing the dragging operation.
This works because UIGestureRecognizer
doesn't block the responder chain.
From the UIGestureRecognizer
documentation:
UIGestureRecognizer objects are not in the responder chain, yet observe touches hit-tested to their view and their view's subviews.
Create a custom UIViewController
(DragAndDropViewController) and add its view to the parent view of the views you want the drag & drop operation to occur in. Use your custom gesture recognizer class to forward the touch information to your DragAndDropViewController.
The source tells your DragAndDropViewController where the the drag originates from (and any custom info). The controller should also have a delegate reference to the drop destination. When the drop occurs, send the delegate the UITouch event (not recommended according to Apple, but the UITouch object is needed to get the correct location in the destination view).
This is what my DragAndDropViewController looks like:
@protocol DragAndDropDestination
- (void)droppedItem:(NSDictionary *)item withTouch:(UITouch *)touch;
@end
@interface DragAndDropViewController : UIViewController
{
id _dropDestination;
NSDictionary *_draggedItem;
UIImageView *_icon;
}
@property (nonatomic, assign) id dropDestination;
@property (nonatomic, retain) NSDictionary *draggedItem;
// Source sends this message
- (void)startDraggingWithItem:(NSDictionary *)item;
@end
Once the destination gets the message, you can use - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
to get the exact view at the drop destination.
Also make sure you disable cancelsTouchesInView
in the gesture recognizer if you want other UI operations to happen normally in your table views.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With