在滑动手势UITableView上获取单元格索引路径

P.J*_*P.J 19 iphone objective-c uitableview ios swipe-gesture

我正在进行滑动手势UITableView并且想知道我的手指当前进入的单元格的索引路径,即从哪个单元格执行的滑动手势.

我需要indexPath显示所选单元格的信息.

提前致谢..

Sri*_*aju 53

那么你基本上需要的是从桌面上轻扫手势获取单元格?对indexPath.你不需要知道.首先定义滑动tableView等 -

UISwipeGestureRecognizer *showExtrasSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(cellSwipe:)];
showExtrasSwipe.direction = UISwipeGestureRecognizerDirectionRight;
[tableView addGestureRecognizer:showExtrasSwipe];
[showExtrasSwipe release];
Run Code Online (Sandbox Code Playgroud)

在此之后,当实际滑动发生时,您需要将处理程序放入其中.为此尝试这个 -

-(void)cellSwipe:(UISwipeGestureRecognizer *)gesture
{
    CGPoint location = [gesture locationInView:tableView];
    NSIndexPath *swipedIndexPath = [tableView indexPathForRowAtPoint:location];
    UITableViewCell *swipedCell  = [tableView cellForRowAtIndexPath:swipedIndexPath];

    //Your own code...
}
Run Code Online (Sandbox Code Playgroud)

所以,我们所做的是第一次附加SwipeGestureRecognizerUITableView(不是UITableViewCell).之后,当滑动发生时UITableView,我首先得到手势发生在哪里的坐标UITableView.接下来,使用这个坐标,我得到了IndexPath滑动发生的行UITableView.最后使用IndexPath我得到了UITableViewCell.真的很简单..

注意:我被问过这么多次.所以加入的,为什么我用这个解释SwipeGestureRecognizerUITableView,而不是每一个人 UITableViewCell.

我可以依附于SwipeGestureRecognizer每个人UITableViewCell.我没有这样做,因为我不得不SwipeGestureRecognizer为每个细胞附加一个单独的.所以,如果我有1000个单元格,我UITableView将不得不创建1000个SwipeGestureRecognizer对象.这个不好.在我的上述方法中,我只创建了一个SwipeGestureRecognizer,就是这样.

  • 当然,它做到了.这段特殊的代码已经看到了数以百万计的滑动! (2认同)