Kokodelo
Kokodelo

Reputation: 436

How to add an object in an UITableView by drag and drop it in?

So on my main View I've got an UITableView. On this view, with a button, I'm able to add an object which I've called: SelectPostIt (which come from my nib SelectPostIt.xib ).

These Post It are draggable and I'm also able to know the coordinate of the point from where I've dropped the PostIt(I thought it could be usefull) (sorry for my school english).

What I want to do is, when I drop my PostIt on a row of my UITableView, it will add it in this row and then it will move with the row when we scroll the UITableView .

Here is how I add a PostIt on my view:

- (IBAction)addNewPostIt:(id)sender {    

SelectPostIt *selectpostit = [[[NSBundle mainBundle] loadNibNamed:@"SelectPostIt" owner:self options:nil] firstObject];
selectpostit.frame = CGRectMake(x, y, 100, 100);

[self.view addSubview:selectpostit];

selectpostit.edited = false;
selectpostit.commentaireSP.editable = NO;
selectpostit.commentaireSP.scrollEnabled = YES;
selectpostit.layer.borderWidth = 1.0f;    
selectpostit.commentaireSP.userInteractionEnabled = NO;

x+=1;
y+=1;}

And here is how I get the coordinates of the drop:

-(IBAction)handlePan:(UIPanGestureRecognizer *)recognizer{
CGPoint translation = [recognizer translationInView:self];
recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x, recognizer.view.center.y + translation.y);

//That's here
if(recognizer.state == UIGestureRecognizerStateEnded){        
    CGPoint centerPointOfView = [recognizer locationInView:self.superview];
    NSLog(@"X - %f, Y - %f",centerPointOfView.x,centerPointOfView.y);
}    

[recognizer setTranslation:CGPointMake(0, 0) inView:self];}

I dont know how to process, any help will be welcome!

Upvotes: 0

Views: 103

Answers (2)

user6198165
user6198165

Reputation:

I think that you still have the same problem, you should try to create some object classes which represent everything you need to be represented. From here, you'll be able to pick up the data of your object and create a new directly in the view with something like a constructor. But you'll have to update everything that you were doing visually in your code now.

Upvotes: 1

phi
phi

Reputation: 10733

If I understand correctly, you want to change the visual state of one of your tableView cells when the user "drags & drops" the "PostIt" view on the cell.

I would recommend to solve this using some sort of model class that represents the contents of each cell. For example, you can have a showsPostIt property and when the user drops the PostIt on one cell, you set the value of that property for that cell to YES. Then you can just call -reloadData or similar in order to visually update your view.

If you don't do it like that and just -addSubview a PostIt view, you will have problems when the tableView recycles its cells (the PostIt view might disappear or appear to the wrong cells).

Upvotes: 1

Related Questions