Подтвердить что ты не робот

Проблема UIGestureRecognizer и UITableViewCell

Я привязываю UISwipeGestureRecognizer к UITableViewCell к методу cellForRowAtIndexPath:, например:

- (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] autorelease];

        UISwipeGestureRecognizer *gesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(didSwipe:)];
        gesture.direction = UISwipeGestureRecognizerDirectionRight;
        [cell.contentView addGestureRecognizer:gesture];
        [gesture release];
    }
    return cell;
}

Однако метод didSwipe всегда получает вызов дважды при успешном ударе. Сначала я думал, что это происходит из-за того, что жест начинается и заканчивается, но если я выхожу из самого жеста GestureRecognizer, они оба находятся в состоянии "Закончено":

-(void)didSwipe:(UIGestureRecognizer *)gestureRecognizer {

    NSLog(@"did swipe called %@", gestureRecognizer);
}

Консоль:

2011-01-05 12:57:43.478 App[20752:207] did swipe called <UISwipeGestureRecognizer: 0x5982fa0; state = Ended; view = <UITableViewCellContentView 0x5982c30>; target= <(action=didSwipe:, target=<RootViewController 0x5e3e080>)>; direction = right>
2011-01-05 12:57:43.480 App[20752:207] did swipe called <UISwipeGestureRecognizer: 0x5982fa0; state = Ended; view = <UITableViewCellContentView 0x5982c30>; target= <(action=didSwipe:, target=<RootViewController 0x5e3e080>)>; direction = right>

Я действительно действительно не знаю, почему. Я пробовал, очевидно, проверять состояние Ended, но это не поможет, поскольку они оба входят как "Ended" в любом случае... Любые идеи?

4b9b3361

Ответ 1

Вместо того, чтобы напрямую добавить распознаватель жестов в ячейку, вы можете добавить его в таблицу в viewDidLoad.

В методе didSwipe -Method вы можете определить затронутую IndexPath и ячейку следующим образом:

-(void)didSwipe:(UIGestureRecognizer *)gestureRecognizer {

  if (gestureRecognizer.state == UIGestureRecognizerStateEnded) {
        CGPoint swipeLocation = [gestureRecognizer locationInView:self.tableView];
        NSIndexPath *swipedIndexPath = [self.tableView indexPathForRowAtPoint:swipeLocation];
        UITableViewCell* swipedCell = [self.tableView cellForRowAtIndexPath:swipedIndexPath];
        // ...
  }
}

Ответ 2

Он будет работать с делегатом приложения

- (void)tableView:(UITableView*)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath
{

// code

}

Ответ 3

У меня была такая же проблема, и я решил ее, отметив "Атрибут прокрутки" в атрибутах вида таблицы.

Мне не нужна прокрутка в моем представлении таблицы, поэтому он не влияет на приложение каким-либо другим способом, за исключением того, что теперь я не получаю первый ответный ответ после жестового перехода.