如何添加将调用
的手势(从左到右)- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle==UITableViewCellEditingStyleDelete)
{
//
}
}
通过在iOS7中对其进行子类化来实现UITableViewCell的方法。
答案 0 :(得分:1)
如果你有NSMutableArray* sourceArray
作为tableView的dataSource,请尝试这样的事情:
- (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];
}
}
答案 1 :(得分:1)
你应该创建从UITableViewCell发送的自定义类,你应该在init中添加UIPanGestureRecognizer:
UIGestureRecognizer* recognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
recognizer.delegate = self;
[self addGestureRecognizer:recognizer];
下一步是覆盖方法gestureRecognizerShouldBegin:
-(BOOL)gestureRecognizerShouldBegin:(UIPanGestureRecognizer *)gestureRecognizer {
CGPoint translation = [gestureRecognizer translationInView:[self superview]];
if (fabsf(translation.x) > fabsf(translation.y)) {
return YES;
}
return NO;
}
并添加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];
}
}
}
您需要使用方法itemToDeleted添加协议:您需要在tableView:commitEditingStyle中实现它。如果您需要更多帮助,请告诉我。 希望这个帮助