简体   繁体   English

SWRevealViewController和TableView-滑动以删除不起作用

[英]SWRevealViewController and TableView - swipe to delete not working

I added the SWRevealViewController to my app, along with the hamburger stack to access my menu. 我将SWRevealViewController以及汉堡包堆栈添加到了我的应用程序中,以访问菜单。 My app has a UITableView for the main application view. 我的应用程序具有用于主应用程序视图的UITableView。 I want to allow users to delete items from the list using the swipe to delete function. 我想允许用户使用滑动删除功能从列表中删除项目。

I enabled the swipe to delete and added a method call to handle this. 我启用了滑动以删除并添加了方法调用来处理此问题。 I noticed that the method never gets called. 我注意到该方法永远不会被调用。 Not sure how to get this to work. 不知道如何使它工作。

Any help would be greatly appreciated. 任何帮助将不胜感激。

I was asked for a code example. 我被要求提供一个代码示例。 I have not made any changes to the SWRevealViewController standard source code. 我尚未对SWRevealViewController标准源代码进行任何更改。 In my code for the TableView, I have added the following: 在我的TableView代码中,添加了以下内容:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }
}

Normally, the code above should show a delete button when you do a right to left swipe. 通常,当您从右向左滑动时,上面的代码应显示一个删除按钮。 However, this is not happening. 但是,这没有发生。 My guess is that the SWRevealViewController is eating the pan gesture recognizer. 我的猜测是SWRevealViewController正在吃平移手势识别器。

Just found a solution after reading a thread of a guy asking a similar question to the maker of this class. 在阅读了一个与这个班级的制造者问类似问题的家伙的话题后,才找到了解决方案。 Whatever class you use to add the gesture recognizers, make it the delegate of the SWRevealController then paste in this method. 无论您使用什么类来添加手势识别器,都应使其成为SWRevealController的委托,然后粘贴此方法。

#pragma mark - SWRevealViewControllerDelegate

- (BOOL)revealControllerPanGestureShouldBegin:(SWRevealViewController *)revealController
{
    float velocity = [revealController.panGestureRecognizer velocityInView:self.view].x;
    if (velocity < 0 && self.revealViewController.frontViewPosition == FrontViewPositionLeft)
        return NO;
    else
        return YES;
}

I have done like this < UIGestureRecognizerDelegate> in .h file in which you are implementing tableview cell delete functionality and in .m file viewDidLoad 我已经在.h文件中完成了<UIGestureRecognizerDelegate>的工作,在该文件中您正在实现tableview单元格删除功能,而在.m文件中则执行了viewDidLoad

[self.view addGestureRecognizer:self.revealViewController.panGestureRecognizer];        
self.revealViewController.panGestureRecognizer.delegate = self;

After that simply use this method 之后,只需使用此方法

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
if ([NSStringFromClass([touch.view class]) isEqualToString:@"UITableViewCellContentView"])
    return NO;
else
    return YES;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM