iphone ios uitableview drag order

iphone - Cómo ser notificado de movimiento de UITableViewCell inicio y fin



ios drag (4)

Acabo de encontrar un truco, ya que Apple reducirá el alfa, podemos usar eso supongo

@interface CustomTableViewCell () @property (nonatomic, assign) BOOL isDragging; @end -(void)draggingWillBegan { //use a delegate method to inform tableview } -(void)draggingDidEnd { //use a delegate method to inform tableview } - (void)layoutSubviews { [super layoutSubviews]; //Since apple wont provide en if(self.alpha <= 0.95 && !self.isDragging){ self.isDragging = YES; [self draggingWillBegan]; } if(self.alpha >= 0.95 && self.isDragging){ self.isDragging = NO; [self draggingDidEnd]; } }

Tengo una UITableView en mi aplicación iOS que se actualiza periódicamente. El usuario también puede mover las filas de la vista de tabla en todo momento (la vista de tabla está siempre en modo de edición).

Quiero detener el temporizador de actualización cuando el usuario comienza a arrastrar una fila y volver a iniciarla cuando se abandona la fila.

La última parte debería ser fácil con moveRowAtIndexPath , pero ¿cómo recibir notificaciones sobre el inicio de arrastre?

¡Gracias!


Me encontré con el mismo problema hace algún tiempo y no encontré una solución. Mientras comencé esta respuesta con una explicación de por qué no se puede hacer, de hecho descubrí cómo se puede hacer. :-)

En resumen: debe crear una subclase personalizada de UITableViewCell . Reemplazar layoutSubviews para adjuntar un UILongPressGestureRecognizer a UITableViewCellReorderControl . Defina un protocolo y use un delegado para informar a quien quiera sobre el estado de arrastre.

CustomTableViewCell.h:

#import <UIKit/UIKit.h> @protocol CustomTableViewCellDelegate; @interface CustomTableViewCell : UITableViewCell { } @property (nonatomic, assign) id <CustomTableViewCellDelegate> delegate; @end @protocol CustomTableViewCellDelegate - (void)CustomTableViewCell:(CustomTableViewCell *)cell isDragging:(BOOL)value; @end

CustomTableViewCell.m:

#import "CustomTableViewCell.h" @implementation CustomTableViewCell @synthesize delegate = _delegate; - (void)handleGesture:(UIGestureRecognizer *)gestureRecognizer { if (gestureRecognizer.state == UIGestureRecognizerStateBegan) { [_delegate CustomTableViewCell:self isDragging:YES]; // Dragging started } else if (gestureRecognizer.state == UIGestureRecognizerStateEnded) { [_delegate CustomTableViewCell:self isDragging:NO]; // Dragging ended } } - (void)layoutSubviews { [super layoutSubviews]; for (UIView *view in self.subviews) { if ([NSStringFromClass ([view class]) rangeOfString:@"ReorderControl"].location != NSNotFound) { // UITableViewCellReorderControl if (view.gestureRecognizers.count == 0) { UILongPressGestureRecognizer *gesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)]; gesture.cancelsTouchesInView = NO; gesture.minimumPressDuration = 0.150; [view addGestureRecognizer:gesture]; } } } } @end

Tenga en cuenta que aunque este código no utiliza ninguna API privada, aún puede dejar de funcionar si Apple cambia su implementación interna (es decir, cambiando el nombre de clase de UITableViewCellReorderControl ).


Su UITableViewDelegate recibirá las siguientes notificaciones en respuesta a las acciones de reordenamiento:

- (void)tableView:(UITableView *)tableView willBeginReorderingRowAtIndexPath:(NSIndexPath *)indexPath; - (void)tableView:(UITableView *)tableView didEndReorderingRowAtIndexPath:(NSIndexPath *)indexPath; - (void)tableView:(UITableView *)tableView didCancelReorderingRowAtIndexPath:(NSIndexPath *)indexPath;


se llama a este método cuando termina de mover celdas:

- (void) tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath