Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enabled drag & drop to an empty section #12

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 23 additions & 4 deletions BVReorderTableView.m
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,6 @@ - (void)longPress:(UILongPressGestureRecognizer *)gesture {
}
}


- (void)updateCurrentLocation:(UILongPressGestureRecognizer *)gesture {

NSIndexPath *indexPath = nil;
Expand All @@ -255,12 +254,32 @@ - (void)updateCurrentLocation:(UILongPressGestureRecognizer *)gesture {
location = [gesture locationInView:self];
indexPath = [self indexPathForRowAtPoint:location];

// if indexPath is nil then table cell has likely been dragged to an empty section. Use the section header and/or footer
// to determine drag location.
BOOL placeholder = NO;
CGFloat placeholderHeight = 44;
if (indexPath == nil)
{
NSInteger numSections = [self numberOfSections];
for (int i = 0; i < numSections; i++)
{
CGRect headerRect = [self rectForHeaderInSection:i];
CGRect footerRect = [self rectForFooterInSection:i];
if (CGRectContainsPoint(headerRect, location) || CGRectContainsPoint(footerRect, location))
{
// The dragged table cell is definitely in the vicinity of an empty section. Create a new indexPath in the empty section (row 0)
indexPath = [NSIndexPath indexPathForRow:0 inSection:i];
placeholder = YES;
}
}
}

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;
NSInteger oldHeight = !placeholder ? [self rectForRowAtIndexPath:self.currentLocationIndexPath].size.height : placeholderHeight;
NSInteger newHeight = !placeholder ? [self rectForRowAtIndexPath:indexPath].size.height : placeholderHeight;

if (indexPath && ![indexPath isEqual:self.currentLocationIndexPath] && [gesture locationInView:[self cellForRowAtIndexPath:indexPath]].y > newHeight - oldHeight) {
[self beginUpdates];
Expand Down Expand Up @@ -309,4 +328,4 @@ - (void)cancelGesture {
longPress.enabled = YES;
}

@end
@end