【发布时间】:2011-10-31 22:57:07
【问题描述】:
我正在UITableView 上做滑动手势,并想知道我的手指当前所在的单元格的索引路径,即从哪个单元格执行滑动手势。
我需要indexPath,因为我必须显示该选定单元格的信息..
提前谢谢..
【问题讨论】:
标签: iphone objective-c ios uitableview swipe-gesture
我正在UITableView 上做滑动手势,并想知道我的手指当前所在的单元格的索引路径,即从哪个单元格执行滑动手势。
我需要indexPath,因为我必须显示该选定单元格的信息..
提前谢谢..
【问题讨论】:
标签: iphone objective-c ios uitableview swipe-gesture
你检查过这个源代码吗? : https://github.com/thermogl/TISwipeableTableView
这真的可能对你有所帮助,这是“可滑动”表格视图的完整实现...
【讨论】:
那么您本质上需要的是从桌面上的滑动手势中获取单元格?对。你不需要知道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 就可以了。
【讨论】:
如果您尝试实现“滑动删除”(水平方向滑动),则无需重新发明轮子。只需在 UITableViewController 委托中取消注释方法 commitEditingStyle:forRowAtIndexPath:。
【讨论】: