Skip to content

Commit

Permalink
Closes #6. Fix dragging issues where cells are variables heights.
Browse files Browse the repository at this point in the history
Previously the delegate had to keep track of the height of the cell
being dragged. Adding the draggingRowHeight property allows easier
manipulation in cases where cells are variable heights.
  • Loading branch information
bvogelzang committed Jul 18, 2013
1 parent 4576ac9 commit 4f6e9ad
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
1 change: 1 addition & 0 deletions BVReorderTableView.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
@interface BVReorderTableView : UITableView

@property (nonatomic, assign) id <ReorderTableViewDelegate> delegate;
@property (nonatomic, assign) CGFloat draggingRowHeight;
@property (nonatomic, assign) BOOL canReorder;

@end
21 changes: 19 additions & 2 deletions BVReorderTableView.m
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ @interface BVReorderTableView ()
@property (nonatomic, strong) NSTimer *scrollingTimer;
@property (nonatomic, assign) CGFloat scrollRate;
@property (nonatomic, strong) NSIndexPath *currentLocationIndexPath;
@property (nonatomic, strong) NSIndexPath *initialIndexPath;
@property (nonatomic, strong) UIImageView *draggingView;
@property (nonatomic, retain) id savedObject;

Expand All @@ -46,7 +47,14 @@ - (void)cancelGesture;
@implementation BVReorderTableView

@dynamic delegate, canReorder;
@synthesize longPress, scrollingTimer, scrollRate, currentLocationIndexPath, draggingView, savedObject;
@synthesize longPress;
@synthesize scrollingTimer;
@synthesize scrollRate;
@synthesize currentLocationIndexPath;
@synthesize draggingView;
@synthesize savedObject;
@synthesize draggingRowHeight;
@synthesize initialIndexPath;

- (id)init {
return [self initWithFrame:CGRectZero];
Expand Down Expand Up @@ -109,6 +117,7 @@ - (void)longPress:(UILongPressGestureRecognizer *)gesture {
if (gesture.state == UIGestureRecognizerStateBegan) {

UITableViewCell *cell = [self cellForRowAtIndexPath:indexPath];
self.draggingRowHeight = cell.frame.size.height;
[cell setSelected:NO animated:NO];
[cell setHighlighted:NO animated:NO];

Expand Down Expand Up @@ -146,6 +155,7 @@ - (void)longPress:(UILongPressGestureRecognizer *)gesture {
[self insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];
self.savedObject = [self.delegate saveObjectAndInsertBlankRowAtIndexPath:indexPath];
self.currentLocationIndexPath = indexPath;
self.initialIndexPath = indexPath;
[self endUpdates];

// enable scrolling for cell
Expand Down Expand Up @@ -231,7 +241,14 @@ - (void)updateCurrentLocation:(UILongPressGestureRecognizer *)gesture {
location = [gesture locationInView:self];
indexPath = [self indexPathForRowAtPoint:location];

if (indexPath && ![indexPath isEqual:self.currentLocationIndexPath]) {
if ([self.delegate respondsToSelector:@selector(tableView:targetIndexPathForMoveFromRowAtIndexPath:toProposedIndexPath:)]) {
indexPath = [self.delegate tableView:self targetIndexPathForMoveFromRowAtIndexPath:self.initialIndexPath toProposedIndexPath:indexPath];
}

NSInteger oldHeight = [self rectForRowAtIndexPath:self.currentLocationIndexPath].size.height;
NSInteger newHeight = [self rectForRowAtIndexPath:indexPath].size.height;

if (indexPath && ![indexPath isEqual:self.currentLocationIndexPath] && [gesture locationInView:[self cellForRowAtIndexPath:indexPath]].y > newHeight - oldHeight) {
[self beginUpdates];
[self deleteRowsAtIndexPaths:[NSArray arrayWithObject:self.currentLocationIndexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
[self insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
Expand Down

0 comments on commit 4f6e9ad

Please sign in to comment.