diff --git a/BVReorderTableView.m b/BVReorderTableView.m index 055f9df..fcc265d 100644 --- a/BVReorderTableView.m +++ b/BVReorderTableView.m @@ -245,7 +245,6 @@ - (void)longPress:(UILongPressGestureRecognizer *)gesture { } } - - (void)updateCurrentLocation:(UILongPressGestureRecognizer *)gesture { NSIndexPath *indexPath = nil; @@ -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]; @@ -309,4 +328,4 @@ - (void)cancelGesture { longPress.enabled = YES; } -@end \ No newline at end of file +@end