Pregunta

Te adjunto un UISwipeGestureRecognizer a un UITableViewCell en el método cellForRowAtIndexPath: este modo:

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

Sin embargo, el método didSwipe siempre llama dos veces en conseguir golpe exitoso. Al principio pensé que esto era porque el gesto comienza y termina, pero si me conecto a cabo el propio gestureRecognizer, ambos se encuentran en el estado "Finalizado":

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

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

Consola:

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>

Yo realmente no sé por qué. He intentado, obviamente, comprobando el estado de terminación, pero eso no ayuda, ya que ambos vienen en como "composición" de todos modos ... ¿Alguna idea?

¿Fue útil?

Solución

En lugar de añadir el reconocedor gesto a la célula directamente, puede agregarlo a la tableview en viewDidLoad.

En el didSwipe-método que se puede determinar la IndexPath y célula afectada de la siguiente manera:

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

Otros consejos

Se trabajará con delegado de la aplicación

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

// code

}

Yo tenía el mismo problema y lo resolvió marcando "Desplazamiento Activado" en los atributos de vista tabla.

Mi vista de tabla no necesita desplazarse, por lo que no afecta a la aplicación de cualquier otro modo, excepto que ahora no consigo el primer grifo que no responde después de un gesto de golpe.

Adición gesto en el método awakeFromNib funciona sin 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 bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top