如何在滑动手势上获取IndexPath和UItableView

时间:2015-10-14 06:18:19

标签: ios objective-c iphone uitableview

我有一个UITableView,其中我添加了UISwipeGesture,如下所示:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

CustomCell *cell = (CustomCell*)[tableView dequeueReusableCellWithIdentifier:kCellIndetifier];

UISwipeGestureRecognizer* gestureR;
gestureR = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom)];
gestureR.direction = UISwipeGestureRecognizerDirectionLeft;
[cell addGestureRecognizer:gestureR];
  return cell;
}

我的选择器在滑动时响应。

现在我想根据SwipeGesture操纵单元格。但我无法获得indexPathUITableView。我怎样才能让它们操纵细胞。或者是他们的任何其他方式。

请帮助我。

4 个答案:

答案 0 :(得分:2)

您可以执行以下操作..

UISwipeGestureRecognizer* gestureR;
gestureR = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)];
gestureR.direction = UISwipeGestureRecognizerDirectionLeft;
[cell addGestureRecognizer:gestureR];


-(void)handleSwipeFrom:(UIGestureRecognizer *)tap{
    CGPoint touch           = [tap locationInView:yourtableview];
    NSIndexPath *indexPath  = [yourtableview indexPathForRowAtPoint:touch];
    CustomCell *cell      = [yourtableview cellForRowAtIndexPath:indexPath];

}

让我知道它是否有效。

快乐编码!!!

答案 1 :(得分:0)

试试这个

 //Modify yourCode
 UISwipeGestureRecognizer* gestureR;
gestureR = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)];


-(void)handleSwipeFrom:(UISwipeGestureRecognizer*)sender
{
CGPoint center= sender.view.center;
CGPoint point = [sender.view.superview convertPoint:center toView:tableViewObj];
NSIndexPath *indexPath = [tableViewObj indexPathForRowAtPoint:point];
  }

答案 2 :(得分:0)

只需在滑动手势视图中添加标记,您就可以在handleSwipeFrom方法中访问该标记

 UISwipeGestureRecognizer* gestureR;
    gestureR = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom)];
    gestureR.direction = UISwipeGestureRecognizerDirectionLeft;
   [cell setTag:indexPath.row]; //set tag value
    [cell addGestureRecognizer:gestureR];


- (void)handleSwipeFrom:(UIGestureRecognizer *)sender
{
    NSLog(@"tabbed!!");
    NSLog(@"%d", sender.view.tag);    //here you will get the index path
}

答案 3 :(得分:0)

http://www.appcoda.com/swipeable-uitableviewcell-tutorial/

我认为这就是你要找的东西。