19

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

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

提前致谢..

4

3 回答 3

54

所以你本质上需要的是从桌子上的滑动手势中获取单元格?对。你不需要知道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...
}

所以我们所做的是首先将 a 附加SwipeGestureRecognizerUITableView(而不是UITableViewCell)。之后,当在 上滑动时UITableView,我首先得到手势在UITableView. 接下来,使用这个坐标,我得到了IndexPathUITableView. 最后使用IndexPath我得到UITableViewCell. 真的很简单。。

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

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

于 2011-08-22T08:23:48.647 回答
0

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

于 2011-08-22T08:46:37.053 回答
-1

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

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

于 2011-08-22T08:07:16.863 回答