Reputation: 5655
How to add a gesture(left to right) which will call
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle==UITableViewCellEditingStyleDelete)
{
//
}
}
method to a UITableViewCell by subclassing it in iOS7.
Upvotes: 0
Views: 122
Reputation: 25459
You should create custom class delivered from UITableViewCell and you should add UIPanGestureRecognizer in init:
UIGestureRecognizer* recognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
recognizer.delegate = self;
[self addGestureRecognizer:recognizer];
The next step is override method gestureRecognizerShouldBegin:
-(BOOL)gestureRecognizerShouldBegin:(UIPanGestureRecognizer *)gestureRecognizer {
CGPoint translation = [gestureRecognizer translationInView:[self superview]];
if (fabsf(translation.x) > fabsf(translation.y)) {
return YES;
}
return NO;
}
and add handlePan:
-(void)handlePan:(UIPanGestureRecognizer *)recognizer {
if (recognizer.state == UIGestureRecognizerStateBegan) {
_originalCenter = self.center; //variable to keep centre
}
if (recognizer.state == UIGestureRecognizerStateChanged) {
CGPoint translation = [recognizer translationInView:self];
//this check out if you drag more than half of the screen width
self.center = CGPointMake(_originalCenter.x + translation.x, _originalCenter.y);
_deleteOnDragRelease = self.frame.origin.x < -self.frame.size.width / 2;
}
if (recognizer.state == UIGestureRecognizerStateEnded) {
CGRect originalFrame = CGRectMake(0, self.frame.origin.y,
self.bounds.size.width, self.bounds.size.height);
if (!_deleteOnDragRelease) {
[UIView animateWithDuration:0.2
animations:^{
//this is for animate that you move the cell but you don't need it (it just look cool)
self.frame = originalFrame;
}
];
}
if (_deleteOnDragRelease) {
// need to implement your delegate
[self.delegate itemToDeleted:self.YOURDATA];
}
}
}
You need to add protocol with method itemToDeleted: and you need to implement it in tableView:commitEditingStyle. Let me know if you need more help with that. Hope this help
Upvotes: 1
Reputation: 3658
If you have NSMutableArray* sourceArray
as dataSource for your tableView, try something like this:
- (void)tableView:(UITableView*)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath*)indexPath
{
if (editingStyle==UITableViewCellEditingStyleDelete)
{
[sourceArray removeObjectAtIndex:indexPath.row];
[tableView beginUpdates];
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
[tableView endUpdates];
}
}
Upvotes: 1