Tutorial on dragging an item from a UITableView to a UITableView

I hit my head about it several times, and I realized that. I want to get the community back since I got a lot of help from this site :).

I am trying to copy an element from one UITableView to another UITableView, and the information I saw on the Internet about how to do this was sketchy at best. I myself understood this and therefore I will describe my little architecture.

  • UIView Wizard
    • UIView with UITableView
      • Custom UITableViewCell
        • Custom UIView that is being copied (Person object in my case)
    • UIView with UITableView
      • Custom UITableViewCell
        • Custom UIView that is being copied (Person object in my case)

The person object that I have in the UITableView is the object I want to drag from one table to another. It was the hardest part for me to figure out how to get an item from the table and drag it in one motion. For the longest time, two touches would be required to complete the operation.

Starting with the Person object, this is a simple object containing an image. I had to implement my own touchesMoved method to change the center position of the face when dragging.

-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{ if( m_moveable == YES ){ UITouch *touch = [touches anyObject]; CGPoint location = [touch locationInView:self.superview]; if( 0 < location.x-50 && location.x+50 < 768 ){ if( 0 < location.y-50 && location.y+150 < 1004 ){ self.center = location; } } } } 

I select the User userInteractionEnabled check box for the NO object to initialize so that no clicks in the table fall into the Person object. The Person object in this case moves inside the table that defeats the target.

The next object is my custom UITableViewCell. This object is responsible for catching the user at the first touch. What he has to do is to catch this touch and "pop" of the Man. A person is one of the subzones belonging to a custom UITableViewCell.

  - (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { UIView *parent = self.superview.superview.superview; Person *s = nil; for( UIView *child in self.subviews ){ if( [child isKindOfClass:[Person class]] ){ s = child; s removeFromSuperview]; break; } } if( s != nil ){ self.userInteractionEnabled = NO; s.userInteractionEnabled = YES; UITableView *subParent = self.superview; //EDIT #1 subParent.scrollEnabled = NO; //EDIT #1 [parent addSubview:s]; //[self touchesEnded:touches withEvent:event]; //EDIT #1 } } 

It is important to note that the userInteractionEnabled flag gets clicked on the above method. Before touching, the Person object is “out of bounds” for the person. After the user cell catches the move, Person is freed by adding it to the parent view and then activating (userInteractionEnabled = YES). The Person object is then “born” and can handle movements of its own.

This has one minor glitch in that the Person object blinks in the upper left corner, but then immediately drops down to the user's finger.

The last part of this project is that the UIView wizard should handle the "touch transition". When the user touches the table and the Person object appears, the application must understand that the focus should be removed from the table and directed to the Person object. The way this was done is that the hitTest method in the main UIView was overloaded with the following.

 - (UIView*) hitTest:(CGPoint)point withEvent:(UIEvent *)event { UIView *rv = nil; for(UIView *child in self.subviews){ if( [child isKindOfClass:[Person class]] ){ rv = child; child.center = point; break; } } if( rv == nil ){ rv = [super hitTest:point withEvent:event]; } return rv; } 

The way this code works is that when a person jumped out of a table, he does not have a touch focused on it. The touch “belongs” to the UITableView from which the Human popped up. The hitTest method is the key to reorienting this touch. The system regularly checks which UIView is in the spotlight. The hitTest method is called by the system to identify the UIView. When Person is attached to the main view, this hitTest function iterates over all subzones and detects the presence of Person and returns it as the “dominant” affected object. Any movement of the finger is immediately reported to the Person, not to the UITableView.

This is the courage to realize. To catch a UITableView, a moving object is simple, and I will leave it to you to try! If you have any questions, please write them!

EDIT # 1 Deleting a Person object is more complicated than I thought :). I had to add a line to prevent the UITableView from scrolling when the parent moved, since the UITableView sucks in all the motion events.
The touchesEnded function is run in the UITableViewCell custom class.
Mj

+46
ios objective-c uitableview drag-and-drop
Aug 14 '10 at 3:10
source share
1 answer

Hi, I managed to create a drag & drop row in a UITableView in another row of the same table. I created a uiImageView representing a moving element (which has not been removed from the table) and attached it to the UIView itself so that it drags it to the full screen. Here are some reports of the pains that I have encountered:

  • UITableView: drag a row to another
  • UITableView: Scrolling Software Content View
  • UITableView: custom gestures no longer scroll

Hope this helps ^^

+7
Aug 21 '10 at 7:01
source share
— -



All Articles