Reputation: 31
I Basically have a UITableViewCell Class with a custom Delete Button appearing when swiping a cell using Pan Gesture.
Something like this. _delBtn is my Delete Button.On clicking that it should delete the TableCell. Please help me on this
- (void)handlePan:(UIPanGestureRecognizer *)recognizer
{
CGPoint translation = [recognizer translationInView:self];
recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x, recognizer.view.center.y);
[recognizer setTranslation:CGPointMake(0, 0) inView:_baseView];
if (recognizer.state == UIGestureRecognizerStateEnded)
{
translation.y = 0;
CGPoint velocity = [recognizer velocityInView:_baseView];
CGFloat magnitude = sqrtf((velocity.x * velocity.x));
CGFloat slideMult = magnitude / 1000;
NSLog(@"magnitude: %f, slideMult: %f", magnitude, slideMult);
float slideFactor = 0.1 * slideMult;
velocity.x = velocity.x + 1000;
velocity.y = 0;
CGPoint finalPoint = CGPointMake(recognizer.view.center.x + (velocity.x * slideFactor),
recognizer.view.center.y + (velocity.y * slideFactor));
finalPoint.x = MIN(MAX(finalPoint.x, 0), _baseView.bounds.size.width);
finalPoint.y = MIN(MAX(finalPoint.y, 0), _baseView.frame.size.height);
[UIView animateWithDuration:slideFactor*2 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
recognizer.view.center = finalPoint;
} completion:nil];
if (finalPoint.x >= 195)
{
_delBtn = [[UIButton alloc] initWithFrame:CGRectMake(5, 10, 60, 60)];
[_delBtn setImage:[UIImage imageNamed:@"delBtn3.jpg"] forState:UIControlStateNormal];
[_delBtn addTarget:self action:@selector(deleteClicked) forControlEvents:UIControlEventTouchUpInside];
_delBtn.hidden = NO;
[_baseView insertSubview:_delBtn atIndex:0];
_delBtn.alpha = 0;
[UIView animateWithDuration:slideFactor delay:0.4 options:UIViewAnimationOptionCurveLinear animations:^{
recognizer.view.center = finalPoint;
} completion:nil];
Upvotes: 1
Views: 457
Reputation: 5579
In your deleteClicked
method, you will need to have the UITableView
delete the cell. The cell will need to have a reference to the UITableView
that contains it as well as knowledge of the row and section it represents in the table.
The UITableView
parent can be obtained with the self.superview
property, but it may be safer to store a weak reference in your own custom property, or better yet, a reference to the UIViewController
class that manages the UITableView
. This way you can simply create a method in the controller that you can call to delete the cell.
You will also need to create a property to store the NSIndexPath
that your UITableViewCell
currently represents in the table, if you don't already have access to this information in some other custom property in the cell class. You will set these properties in the UITableViewDataSource
method tableView:cellForRowAtIndexPath:
when you initially create or deque the UITableViewCell
.
In your deleteClicked
method, you can call the method in your UIViewController
, something like deleteCellAtIndexPath:
, passing it the NSIndexPath
of the cell that needs deleting.
In this deleteCellAtIndexPath:
you will do two things. First, amend the backing data source for your UITableView
by removing the data object for the cell to be deleted. This is likely some sort of NSMutableArray
.
After removing the backing data, call the method deleteRowsAtIndexPaths:withRowAnimation:
on the UITableView
, passing it the NSIndexPath
as the only object in an NSArray
. Your cell should then be deleted, animating it as specified with the UITableViewRowAnimation
parameter. See the UITableView
documentation for more information regarding these methods.
Upvotes: 1