我正在做滑动手势UITableView
并想知道我的手指当前所在的单元格的索引路径,即从哪个单元格执行滑动手势。
我需要indexPath
,因为我必须显示该选定单元格的信息..
提前致谢..
我正在做滑动手势UITableView
并想知道我的手指当前所在的单元格的索引路径,即从哪个单元格执行滑动手势。
我需要indexPath
,因为我必须显示该选定单元格的信息..
提前致谢..
所以你本质上需要的是从桌子上的滑动手势中获取单元格?对。你不需要知道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 附加SwipeGestureRecognizer
到UITableView
(而不是UITableViewCell
)。之后,当在 上滑动时UITableView
,我首先得到手势在UITableView
. 接下来,使用这个坐标,我得到了IndexPath
在UITableView
. 最后使用IndexPath
我得到UITableViewCell
. 真的很简单。。
注意:我被问过太多次了。所以添加这个解释为什么我在每个人SwipeGestureRecognizer
上UITableView
而不是在每个人上 使用UITableViewCell
。
我本可以附加SwipeGestureRecognizer
到每个UITableViewCell
. 我没有这样做,因为我必须SwipeGestureRecognizer
为每个单元格单独附加一个。所以如果我有 1000 个单元格,我UITableView
将不得不创建 1000 个SwipeGestureRecognizer
对象。情况不妙。在我上面的方法中,我只是创建一个SwipeGestureRecognizer
,就是这样。
如果您尝试实现“滑动删除”(水平方向滑动),则无需重新发明轮子。只需取消注释commitEditingStyle:forRowAtIndexPath:
您的UITableViewController
委托中的方法。
你检查过这个源代码吗?: https ://github.com/thermogl/TISwipeableTableView
这真的可能对您有所帮助,这是“可滑动”表格视图的完整实现......