Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an easy way to drag and drop a UITableViewCell between two UITableViews?

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?

like image 917
Steve Simitzis Avatar asked Mar 26 '10 19:03

Steve Simitzis


People also ask

How do you add a space between UITableView cells?

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.

What class does UITableView inherit from?

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.

What are delegate and datasource methods of UITableView?

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..


1 Answers

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.

like image 105
Morrowless Avatar answered Oct 10 '22 00:10

Morrowless