Вопрос

Я прикрепляю 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 Метод всегда вызывается дважды на успешном промахе. Я изначально подумал, что это потому, что жест заканчивается и заканчивается, но если я выхожу в систему GestureCongognizer, они оба в состоянии «закончились»:

-(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>

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

Это было полезно?

Решение

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

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

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

Другие советы

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

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

// code

}

У меня была та же проблема и решила его, отмечая «Прокрутка включена» в атрибутах представления таблицы.

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

Добавление жеста в методе AwekeFromnib работает без проблем.

class TestCell: UITableViewCell {

    override func awakeFromNib() {
        super.awakeFromNib()

        let panGesture = UIPanGestureRecognizer(target: self,
                                            action: #selector(gestureAction))
        addGestureRecognizer(panGesture)
    }

    @objc func gestureAction() {
        print("gesture action")
    }
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top