【问题标题】:detect swiping left on a cell in a tableView检测在 tableView 中的单元格上向左滑动
【发布时间】:2015-09-25 13:16:43
【问题描述】:

我正在尝试检测左滑动,所以在开始时添加到我的表格视图中:

  //swipe left
        UISwipeGestureRecognizer *recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self
                                                                                         action:@selector(leftSwipe:)];
        [recognizer setDirection:(UISwipeGestureRecognizerDirectionLeft)];
        [self.tableView addGestureRecognizer:recognizer];

然后用 :

检测单元格本身
- (void)leftSwipe:(UISwipeGestureRecognizer *)gestureRecognizer
{

        CGPoint location = [gestureRecognizer locationInView:self.tableView];
        NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:location];
        UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
       //show some menu in the cell position( its cell.frame)
}

它仅在 tableview 尚未向下滚动时才有效,这意味着在我向下滚动后,我检测到的检测位置是错误的,因此如果我尝试添加一些侧边菜单,它的位置不正确。

如果我一开始就选中这个,不滚动,效果很好。

为什么会这样?

【问题讨论】:

标签: ios objective-c uitableview


【解决方案1】:

解决了。当我添加视图时,我将它放在视图中,而不是在 tableView 中,因此位置错误,因为它们是相对于滚动条而不是视图本身计算的。

将菜单添加到tableView,放在正确的位置。

【讨论】:

    【解决方案2】:
    -(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
        static NSString *CellIdentifier = @"LazyTableCell";
    
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil)
        {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
            cell.backgroundColor=[UIColor clearColor];
            if (tableView == tableviewMsg)
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
    
        else
        {
            for (UIView *view in cell.contentView.subviews)
                [view removeFromSuperview];
        }
        UIView *SwipeView = [[UIView alloc]init];
        SwipeView.frame = CGRectMake(0,0,394,50);
        SwipeView.tag = -1001;
        [SwipeView setBackgroundColor:[UIColor clearColor]];
        [SwipeView setUserInteractionEnabled:YES];
        [cell.contentView addSubview:SwipeView];
    
            if([indexPath isEqual:indexSelected])
            {
    
                CGRect rect = SwipeView.frame;
                rect.origin.x = -45;
                SwipeView.frame = rect;
            }
    
            UISwipeGestureRecognizer* swipeGestureLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(cellSwipedLeft:)];
            [swipeGestureLeft setDirection:UISwipeGestureRecognizerDirectionLeft];
            [cell addGestureRecognizer:swipeGestureLeft];
    
    
            UISwipeGestureRecognizer* swipeGestureRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(cellSwipedRight:)];
            [swipeGestureRight setDirection:UISwipeGestureRecognizerDirectionRight];
            [cell addGestureRecognizer:swipeGestureRight];
    
    
    
        cell.backgroundColor=[UIColor whiteColor];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    
    }
    - (void)cellSwipedLeft:(UIGestureRecognizer *)gestureRecognizer {
    
    
        if (gestureRecognizer.state == UIGestureRecognizerStateEnded) {
            UITableViewCell *cell = (UITableViewCell *)gestureRecognizer.view;
            NSIndexPath* indexPath = [tableviewMsg indexPathForCell:cell];
    
    
            if ([indexPath isEqual:indexSelected]) {
    
    
            }
            else{
    
                [self hideButtonForIndex:indexSelected animated:YES];
    
                [self showButtonForIndex:indexPath animated:YES];
    
            }
    
        }
    }
    - (void)cellSwipedRight:(UIGestureRecognizer *)gestureRecognizer {
    
        if (gestureRecognizer.state == UIGestureRecognizerStateEnded)
        {
            UITableViewCell *cell = (UITableViewCell *)gestureRecognizer.view;
            NSIndexPath* indexPath = [tableviewMsg indexPathForCell:cell];
            if ([indexPath isEqual:indexSelected])
            {
                [self hideButtonForIndex:indexSelected animated:YES];
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2018-02-01
      • 2023-03-22
      • 2011-07-23
      • 2016-07-30
      • 1970-01-01
      • 2012-09-12
      • 2017-07-01
      • 1970-01-01
      相关资源
      最近更新 更多