2015-09-25 54 views
0

我試圖發現左輕掃,在開始所以加入到我的tableview:檢測向左滑動的單元格中的tableView

//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) 
} 

它只有在桌面視圖沒有向下滾動的情況下才有效,這意味着在向下滾動之後,我檢測到的檢測位置是錯誤的,因此如果我嘗試添加一些側面菜單,它沒有定位正確。

如果我在開始時檢查這個權利,不滾動,它很好。

這是爲什麼?

+0

SwTableviewCell實現了可滑動的內容視圖。這將幫助你在單元格https://github.com/CEWendel/SWTableViewCell上添加滑動條 – vijeesh

回答

0

已解決。當我添加視圖時,我將它放在視圖中,而不是放在tableView中,因此位置出錯,因爲它們是相對於滾動器計算的,而不是它自己的視圖。

將菜單添加到tableView,把它放在正確的地方。

0
-(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]; 
     } 
    } 
}