How to get notified of UITableViewCell move start and end?
Your UITableViewDelegate will receive the following notifications in response to reordering actions:
- (void)tableView:(UITableView *)tableView willBeginReorderingRowAtIndexPath:(NSIndexPath *)indexPath;
- (void)tableView:(UITableView *)tableView didEndReorderingRowAtIndexPath:(NSIndexPath *)indexPath;
- (void)tableView:(UITableView *)tableView didCancelReorderingRowAtIndexPath:(NSIndexPath *)indexPath;
Just I found a hack, since apple will reduce the alpha, we can use that i guess
@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];
}
}
I ran into the same problem some time ago and didn't find a solution. While I started this answer with an explanation why it can't be done, I actually found out how it can be done! :-)
In short: You have to create a custom subclass of UITableViewCell
. Override layoutSubviews
to attach a UILongPressGestureRecognizer
to UITableViewCellReorderControl
. Define a protocol and use a delegate to inform whoever you want to about the dragging state.
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
Be aware that while this code doesn't use any private APIs it still might stop working if Apple changes its internal implementation (i.e. by changing the classname of UITableViewCellReorderControl
).