长按手势获取UITableView的indexPath值,删除某行

来源:互联网 发布:西安科技大学考研 知乎 编辑:程序博客网 时间:2024/05/18 01:25

- (void)viewDidLoad

{

//给cell加长按手势

UILongPressGestureRecognizer *gestureLongPress = [[UILongPressGestureRecognizeralloc]initWithTarget:selfaction:@selector(gestureLongPress:)];

gestureLongPress.minimumPressDuration =1;

[self.tableV0addGestureRecognizer:gestureLongPress];

[gestureLongPressrelease];

}




#pragma mark - 长按手势


- (void)gestureLongPress:(UILongPressGestureRecognizer *)gestureRecognizer

{

   CGPoint tmpPointTouch = [gestureRecognizer locationInView:self.tableV0];

   if (gestureRecognizer.state ==UIGestureRecognizerStateBegan) {

       NSIndexPath *indexPath = [self.tableV0indexPathForRowAtPoint:tmpPointTouch];

       if (indexPath == nil) {

            NSLog(@"not tableView");

        }else{

           focusSection = [indexPath section];

           focusRow = [indexPath row];

            

           NSLog(@"%d",focusSection);

           NSLog(@"%d",focusRow);

            

            

           deletebtn.hidden =NO;

        }

    }

}




//删除

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:focusRow inSection:focusSection];

[self.tableV0 deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];




之前见过见过这样的函数:CGRectContainsPoint() // 用来判断一个点 是否在矩形区域内

具体点:

//隐藏

-(void)dismiss:(UITapGestureRecognizer *)tap{

    if(CGRectContainsPoint(self.frame, [taplocationInView:_buttomView])) {  // 注意tap获取点的View一定要大于你要判断的矩形区域,否则有bug;  

        NSLog(@"tap");    ///  参数: 区域范围(CGrect),判别的区域(CGpoint)   如果点到了判别区域slog,否则使消失

    } else{

        

        [selfdismissBlock:^(BOOL complete) {

            

            

            

        }];

    }

}





//删除

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:focusRow inSection:focusSection];

[self.tableV0 deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];


0 0
原创粉丝点击