I have a UITableView in my iOS app that gets refreshed periodically. The user is also able to move the tableview rows at all times (the tableview is always in editing mode).
I want to stop the refresh timer when the user starts dragging a row and start it again when the row is dropped.
The last part should be easy with moveRowAtIndexPath
, but how to get notified about drag start?
When [tableView reloadData] returns, the internal data structures behind the tableView have been updated. Therefore, when the method completes you can safely scroll to the bottom.
UITableView manages the basic appearance of the table, but your app provides the cells ( UITableViewCell objects) that display the actual content. The standard cell configurations display a simple combination of text and images, but you can define custom cells that display any content you want.
A table view displays a single column of vertically scrolling content, divided into rows and sections. Each row of a table displays a single piece of information related to your app. Sections let you group related rows together. For example, the Contacts app uses a table to display the names of the user's contacts.
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;
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
).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With