Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get notified of UITableViewCell move start and end?

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?

like image 303
Cornelius Avatar asked Mar 27 '12 22:03

Cornelius


People also ask

How do I know if Iitableview has completed reloadData?

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.

How does UITableView work?

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.

What is tableview?

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.


2 Answers

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; 
like image 180
marcprux Avatar answered Oct 04 '22 17:10

marcprux


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).

like image 34
Andy Friese Avatar answered Oct 04 '22 19:10

Andy Friese