Objective c 在UITableViewCell中的任意位置向左或向右滑动以删除该单元格,而不使用删除按钮?

Objective c 在UITableViewCell中的任意位置向左或向右滑动以删除该单元格,而不使用删除按钮?,objective-c,ios,uitableview,Objective C,Ios,Uitableview,我希望能够在表格视图单元格中的任意位置向左或向右滑动,以便在不显示“删除”按钮的情况下,通过动画擦除的单元格。我怎样才能做到这一点?为此,您必须在项目中添加此代码 // Override to support conditional editing of the table view. - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { // Return

我希望能够在表格视图单元格中的任意位置向左或向右滑动,以便在不显示“删除”按钮的情况下,通过动画擦除的单元格。我怎样才能做到这一点?

为此,您必须在项目中添加此代码

// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return YES if you want the specified item to be editable.
    return YES;
}

// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        //add code here for when you hit delete
    }    
}
否则mor信息你可以通过这个链接 实施

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath;

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath;
这两种方法,如果UITableViewCellEditingStyle是UITableViewCellEditingStyleDelete,则使用

- (void)deleteRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;

仅此而已。

我尚未尝试并实现此功能,但我会尝试一下。首先,创建一个自定义UITableViewCell,并让它具有两个可以使用的属性

  • 对正在使用它的tableView的引用
  • 在tableView中查找单元格的indexPath。(注意,每次删除此更改所在的所有单元格的单元格时,都需要更新此参数)
  • 在创建自定义单元格的
    单元格中,设置这些属性。还要向单元格中添加UISweepGestureRecognitor

    cell.tableView=tableView;
    cell.indexPath=indexPath;
    UISwipeGestureRecognizer *swipeGestureRecognizer=[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(deleteCell:)];
    [cell addGestureRecognizer:swipeGestureRecognizer];
    [swipeGestureRecognizer release];
    
    确保手势只接受水平滑动

    -(BOOL) gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
    {
        if([[gestureRecognizer view] isKindOfClass:[UITableViewCell class]]&&
           ((UISwipeGestureRecognizer*)gestureRecognizer.direction==UISwipeGestureRecognizerDirectionLeft
            ||(UISwipeGestureRecognizer*)gestureRecognizer.direction==UISwipeGestureRecognizerDirectionRight)) return YES;
    }
    
    在您的
    deleteCell:

    -(void) deleteCell:(UIGestureRecognizer*)gestureRec
    {
        UIGestureRecognizer *swipeGestureRecognizer=(UISwipeGestureRecognizer*)gestureRec;
        CustomCell *cell=[swipeGestureRecognizer view];
        UITableView *tableView=cell.tableView;
        NSIndexPath *indexPath=cell.indexPath;
        //you can now use these two to perform delete operation
    }
    

    @MadhavanRP发布的解决方案是可行的,但它比需要的更复杂。您可以采用一种更简单的方法,创建一个手势识别器来处理表中发生的所有滑动,然后获取滑动的位置以确定用户滑动的单元格

    要设置手势识别器,请执行以下操作:

    - (void)setUpLeftSwipe {
        UISwipeGestureRecognizer *recognizer;
        recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self
                                                           action:@selector(swipeLeft:)];
        [recognizer setDirection:UISwipeGestureRecognizerDirectionLeft];
        [self.tableView addGestureRecognizer:recognizer];
        recognizer.delegate = self;
    }
    
    viewDidLoad

    要处理刷卡,请执行以下操作:

    - (void)swipeLeft:(UISwipeGestureRecognizer *)gestureRecognizer {
        CGPoint location = [gestureRecognizer locationInView:self.tableView];
        NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:location];
        ... do something with cell now that i have the indexpath, maybe save the world? ...
    }
    

    注意:您的vc必须实现手势识别器委托。

    Vivek2012和我会-感谢您的回复,但我不想显示删除按钮,stackoverflow中已经有这些问题的答案。我希望手机在收到向左或向右滑动的手势时立即被删除。请阅读我的描述谢谢不要忘记,在初始化手势识别器以将其添加到单元格时,您需要设置手势识别器的方向。类似于这样的内容:swipgesturerecognizer.direction=UISwipgesturerecognizerDirectionRight;苹果,该死的你没有提供
    (void)取消以前的PerformRequestswithTarget:(id)或Target selector:(SEL)或selector