Pergunta

Estou anexando um UISwipeGestureRecognizer para um UITableViewCell no cellForRowAtIndexPath: Método como assim:

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

No entanto, o didSwipe O método está sempre sendo chamado duas vezes com um golpe bem -sucedido. Inicialmente, pensei que isso era porque o gesto começa e termina, mas se eu registro o próprio GestureRecognizer, ambos estão no estado "terminado":

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

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

Console:

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>

Eu realmente não sei por quê. Eu tentei obviamente verificar o estado final, mas isso não ajuda, pois ambos chegam como "terminaram" de qualquer maneira ... alguma idéia?

Foi útil?

Solução

Em vez de adicionar o reconhecedor de gestos diretamente à célula, você pode adicioná -lo à TableView em viewDidLoad.

No didSwipe-Metódico Você pode determinar o IndexPath e a célula afetados da seguinte forma:

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

Outras dicas

Funcionará com o delegado do aplicativo

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

// code

}

Eu tive o mesmo problema e o resolvi marcando "rolagem ativada" nos atributos da visualização da tabela.

Minha visualização da tabela não precisa rolar, por isso não afeta o aplicativo de nenhuma outra maneira, exceto agora que não recebo o primeiro toque que não responde após um gesto de furto.

Adicionar gestos no método AwakeFromnib funciona sem problemas.

class TestCell: UITableViewCell {

    override func awakeFromNib() {
        super.awakeFromNib()

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

    @objc func gestureAction() {
        print("gesture action")
    }
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top