UITableViewCell drag event detection

Possible duplicate:
How to get notified when UITableViewCell has started and finished moving

I have implemented UITableView

with custom design. This one UITableView

should support edit mode. When entering edit mode, it UITableViewCell

gets additional controls (EditControl, ReorderControl ...). They don't fit well with my custom design, so I wanted to replace their images. For this purpose, I subclassed UITableViewCell

and overrode layoutSubview

where I replace images for these controls.

Problem: When starting a drag-and-drop operation, the image for the EditControl is replaced back to its original somewhere in the UITableViewCell. I can replace it again in



when the user moves the dragged cell to another indexPath, but it's too late.

So my question boils down to this: How can I detect when the user starts dragging UITableViewCell



source to share

1 answer

While Bala's comment went in the right direction, I initially had problems getting it to implement correctly. Now I have learned how this can be done. In short: you need to create your own subclass UITableViewCell

. Override layoutSubviews

to add UILongPressGestureRecognizer

to UITableViewCellReorderControl

. Define a protocol and use a delegate to communicate to whoever you are talking about the state of the drag.


#import <UIKit/UIKit.h>

@protocol CustomTableViewCellDelegate;

@interface CustomTableViewCell : UITableViewCell {

@property (nonatomic, assign) id <CustomTableViewCellDelegate> delegate;


@protocol CustomTableViewCellDelegate
- (void)CustomTableViewCell:(CustomTableViewCell *)cell isDragging:(BOOL)value;



#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];



Please be aware that while this code does not use any private APIs, it may still fail if Apple changes its internal implementation (i.e. changing the class name UITableViewCellReorderControl




All Articles