Skip to content
This repository was archived by the owner on Nov 6, 2018. It is now read-only.

Commit 49152f1

Browse files
author
Florian Mielke
committed
Added check to ask for permission for a row to be moved
1 parent a60308c commit 49152f1

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed

FMFramework/FMMoveTableView.h

+8
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
//
88

99

10+
#import <QuartzCore/QuartzCore.h>
11+
12+
1013
@class FMMoveTableView;
1114

1215

@@ -29,6 +32,11 @@
2932
// Called after the particular row is being dropped to it's new index path
3033
- (void)moveTableView:(FMMoveTableView *)tableView moveRowFromIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath;
3134

35+
@optional
36+
37+
// Allows to reorder a particular row
38+
- (BOOL)moveTableView:(FMMoveTableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath;
39+
3240
@end
3341

3442

FMFramework/FMMoveTableView.m

+22
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ @implementation FMMoveTableView
119119
- (void)setup
120120
{
121121
UILongPressGestureRecognizer *movingGestureRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)];
122+
[movingGestureRecognizer setDelegate:self];
122123
[self addGestureRecognizer:movingGestureRecognizer];
123124
[self setMovingGestureRecognizer:movingGestureRecognizer];
124125
}
@@ -210,6 +211,27 @@ - (NSIndexPath *)adaptedIndexPathForRowAtIndexPath:(NSIndexPath *)indexPath
210211
#pragma mark -
211212
#pragma mark Handle long press
212213

214+
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
215+
{
216+
BOOL shouldBegin = YES;
217+
218+
if ([gestureRecognizer isEqual:[self movingGestureRecognizer]])
219+
{
220+
// Ask the data source if we are allowed to move the touched row
221+
if ([[self dataSource] respondsToSelector:@selector(moveTableView:canMoveRowAtIndexPath:)])
222+
{
223+
// Grap the touched index path
224+
CGPoint touchPoint = [gestureRecognizer locationInView:self];
225+
NSIndexPath *touchedIndexPath = [self indexPathForRowAtPoint:touchPoint];
226+
227+
shouldBegin = [[self dataSource] moveTableView:self canMoveRowAtIndexPath:touchedIndexPath];
228+
}
229+
}
230+
231+
return shouldBegin;
232+
}
233+
234+
213235
- (void)handleLongPress:(UILongPressGestureRecognizer *)gestureRecognizer
214236
{
215237
switch ([gestureRecognizer state])

FMMoveTable/Classes/FMViewController.m

+6
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,12 @@ - (UITableViewCell *)tableView:(FMMoveTableView *)tableView cellForRowAtIndexPat
113113
}
114114

115115

116+
- (BOOL)moveTableView:(FMMoveTableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
117+
{
118+
return YES;
119+
}
120+
121+
116122
- (void)moveTableView:(FMMoveTableView *)tableView moveRowFromIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
117123
{
118124
NSArray *movie = [[[self movies] objectAtIndex:[fromIndexPath section]] objectAtIndex:[fromIndexPath row]];

0 commit comments

Comments
 (0)