【问题标题】:Getting cell indexpath on swipe gesture UITableView在滑动手势 UITableView 上获取单元格索引路径
【发布时间】:2011-10-31 22:57:07
【问题描述】:

我正在UITableView 上做滑动手势,并想知道我的手指当前所在的单元格的索引路径,即从哪个单元格执行滑动手势。

我需要indexPath,因为我必须显示该选定单元格的信息..

提前谢谢..

【问题讨论】:

    标签: iphone objective-c ios uitableview swipe-gesture


    【解决方案1】:

    你检查过这个源代码吗? : https://github.com/thermogl/TISwipeableTableView

    这真的可能对你有所帮助,这是“可滑动”表格视图的完整实现...

    【讨论】:

      【解决方案2】:

      那么您本质上需要的是从桌面上的滑动手势中获取单元格?对。你不需要知道indexPath。首先像这样定义tableView上的滑动-

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

      之后,当实际滑动发生时,您需要为其添加处理程序。为此试试这个-

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

      所以我们首先将SwipeGestureRecognizer 附加到UITableView(不是UITableViewCell)。之后,当UITableView 上发生滑动时,我首先得到UITableView 中手势发生位置的坐标。接下来,使用此坐标,我得到UITableView 中发生滑动的行的IndexPath。最后使用IndexPath 我得到UITableViewCell。真的很简单..

      注意:我被问过太多次了。所以添加这个解释为什么我在UITableView而不是每个人UITableViewCell上使用SwipeGestureRecognizer

      我可以将SwipeGestureRecognizer 附加到每个UITableViewCell。我没有这样做,因为我必须为每个单元格附加一个单独的SwipeGestureRecognizer。因此,如果我的 UITableView 中有 1000 个单元格,我将不得不创建 1000 个 SwipeGestureRecognizer 对象。这是不好的。在我上面的方法中,我只创建了一个 SwipeGestureRecognizer 就可以了。

      【讨论】:

      • 当然可以。这段特殊的代码已经被刷了数百万次!
      • 我觉得你需要把 pinchedIndexPath 改成 swipedIndexPath
      • 我在我的表格视图中分配了这个代码,这是水平表格视图样式(表格视图中的表格视图)的实现,但滑动不会触发?确实选择单元格有效,所以我知道我正在为右表输入代码....但没有乐趣?
      【解决方案3】:

      如果您尝试实现“滑动删除”(水平方向滑动),则无需重新发明轮子。只需在 UITableViewController 委托中取消注释方法 commitEditingStyle:forRowAtIndexPath:

      【讨论】:

        猜你喜欢
        • 2011-02-02
        • 1970-01-01
        • 1970-01-01
        • 2023-04-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多