2016-05-19 1 views
0

Я использую SWTableViewCell, и я хочу знать действие, которое запускается, когда пользователь салфет влево или вправо.UITableViewCell Как я могу обнаружить, что пользователь нажал влево или вправо?

+0

Проверить это HTTP : //stackoverflow.com/questions/6167756/how-to-detect-a-swipe-to-delete-gesture-in-a-customized-uitableviewcell – kb920

+0

Этот подход только распознает состояние 'UIGestureRecognizerStateEnded' –

+0

@ahmedlabib, у него есть несколько опций для' UIGestureRecognizerState', как началось, отменено, изменилось, закончилось и т. Д. –

ответ

4

вы должны добавить Gesture распознавателя в вас cellForRowAtIndexPath

UISwipeGestureRecognizer* swRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(cellSwipedRight:)]; 
[swRight setDirection:UISwipeGestureRecognizerDirectionRight]; 
[cell addGestureRecognizer:swRight]; 

UISwipeGestureRecognizer* swLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(cellSwipedLeft:)]; 
[swLeft setDirection:UISwipeGestureRecognizerDirectionLeft]; 
[cell addGestureRecognizer:swLeft]; 

, а затем его метод селектор

-(void)cellSwipedRight:(UIGestureRecognizer *)gestureRecognizer 
{ 
    if (gestureRecognizer.state == UIGestureRecognizerStateEnded) { 
     // your code 
    } 
} 

-(void)cellSwipedLeft:(UIGestureRecognizer *)gestureRecognizer 
{ 
    if (gestureRecognizer.state == UIGestureRecognizerStateEnded) { 
     // your code 
    } 
} 
+0

, как я сказал @ kb920 - Этот подход распознает только состояние 'UIGestureRecognizerStateEnded'. –

+0

Нет ... он распознает множественное состояние, когда я комментирую ваш вопрос 'UIGestureRecognizerStateBegan',' UIGestureRecognizerStateCancelled', 'UIGestureRecognizerStateChanged',' UIGestureRecognizerStateEnded' и т. Д. –

+0

Он работал благодаря вам, а также нашел другое решение для этого [SWTableViewCell] (https://github.com/CEWendel/SWTableViewCell) Библиотека '- (void) swipeableTableViewCell: (SWTableViewCell *) cell scrollingToState: (SWCellState) состояние;' –

1

Попробуйте этот код он будет работает для вас:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 

    UISwipeGestureRecognizer* swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(cellSwiped:)]; 
    [swipe setDirection:UISwipeGestureRecognizerDirectionRight]; 
    [cell addGestureRecognizer:swipe]; 

    cell.textLabel.text = [NSString stringWithFormat:@"Cell %d", indexPath.row]; 


return cell; 
    } 


    - (void)cellSwiped:(UIGestureRecognizer *)gestureRecognizer 
    { 
     if (gestureRecognizer.state == UIGestureRecognizerStateEnded) 
     { 
      UITableViewCell *cell = (UITableViewCell *)gestureRecognizer.view; 
      NSIndexPath* indexPath = [self.tableView indexPathForCell:cell]; 
      //.. 
     } 
    } 
Смежные вопросы