Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Support for dragging out rows from tableview

  • Loading branch information...
commit 80fed5b07723af7eb9fced976117932e27702755 1 parent 62faee4
@michaelvillar authored
View
33 lib/UIKit/TUITableView+Cell.m
@@ -49,6 +49,39 @@ -(void)__mouseUpInCell:(TUITableViewCell *)cell offset:(CGPoint)offset event:(NS
* If reordering is permitted by the table, this will begin a move operation.
*/
-(void)__mouseDraggedCell:(TUITableViewCell *)cell offset:(CGPoint)offset event:(NSEvent *)event {
+ BOOL allowDraggingOut = NO;
+ TUIFastIndexPath *indexPath;
+ if([self.delegate respondsToSelector:@selector(tableView:shouldAllowDraggingOutAtIndexPath:)]
+ && [self.delegate respondsToSelector:@selector(tableView:dragOutAtIndexPath:event:offset:)])
+ {
+ indexPath = [self indexPathForCell:cell];
+ allowDraggingOut = [self.delegate tableView:self shouldAllowDraggingOutAtIndexPath:indexPath];
+ }
+
+ if(allowDraggingOut) {
+ CGPoint point = [self convertPoint:event.locationInWindow
+ fromView:nil];
+ if(point.x < -20 || point.x > self.bounds.size.width + 20 ||
+ point.y < -20 || point.y > self.bounds.size.height + 20)
+ {
+ cell.highlighted = NO;
+ if([self __isDraggingCell]) {
+ [self reloadData];
+ // clear state
+ _currentDragToReorderIndexPath = nil;
+
+ _previousDragToReorderIndexPath = nil;
+
+ // and clean up
+ _dragToReorderCell = nil;
+
+ _currentDragToReorderLocation = CGPointZero;
+ _currentDragToReorderMouseOffset = CGPointZero;
+ }
+ [self.delegate tableView:self dragOutAtIndexPath:indexPath event:event offset:offset];
+ return;
+ }
+ }
[self __updateDraggingCell:cell offset:offset location:[[cell superview] localPointForEvent:event]];
}
View
7 lib/UIKit/TUITableView.h
@@ -61,6 +61,13 @@ typedef enum {
// the following is optional for row reordering
- (TUIFastIndexPath *)tableView:(TUITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(TUIFastIndexPath *)fromPath toProposedIndexPath:(TUIFastIndexPath *)proposedPath;
+// optional for dragging out of the tableview (both are required for dragging out to be supported)
+- (BOOL)tableView:(TUITableView *)tableView shouldAllowDraggingOutAtIndexPath:(TUIFastIndexPath *)indexPath;
+- (void)tableView:(TUITableView *)tableView
+dragOutAtIndexPath:(TUIFastIndexPath *)indexPath
+ event:(NSEvent *)event
+ offset:(CGPoint)offset;
+
@end
@interface TUITableView : TUIScrollView
View
2  lib/UIKit/TUITableViewCell.h
@@ -46,7 +46,7 @@ typedef enum {
@property (weak, nonatomic, readonly) TUITableView *tableView;
@property (strong, nonatomic, readonly) TUIFastIndexPath *indexPath;
-@property (nonatomic, readonly, getter=isHighlighted) BOOL highlighted;
+@property (nonatomic, readwrite, getter=isHighlighted) BOOL highlighted;
@property (nonatomic, assign, getter=isSelected) BOOL selected;
- (void)setSelected:(BOOL)s animated:(BOOL)animated; // called by table view (don't call directly). subclasses can override
View
6 lib/UIKit/TUITableViewCell.m
@@ -176,6 +176,12 @@ - (BOOL)isHighlighted
return _tableViewCellFlags.highlighted;
}
+- (void)setHighlighted:(BOOL)highlighted
+{
+ _tableViewCellFlags.highlighted = highlighted;
+ [self setNeedsDisplay];
+}
+
- (BOOL)isSelected
{
return _tableViewCellFlags.selected;

4 comments on commit 80fed5b

@galaxas0

I would like to add that this only sends the message when the mouse exits the cell boundaries during drag... while I expected it to function similarly to Android's notifications where you could swipe left or right and it would move the x position of the cell and notify during the drag. Android's version also only allows one way of dragging- if you start in x pos (drag out) drag, it would disable y pos (reorder) drag and vice versa.

Was this the expected function or am I missing something here?

@galaxas0

And secondly, does the table view's -reloadData method not jumble up your cells?

@michaelvillar

ReloadData works fine over here.

Yes it only sends the message, the delegate should manage to create the dragging stuff.

@galaxas0

Okay, I've tracked the issue to -_layoutCells, which flips the array of cells (indexPathsToAdd, presumably) each time it's re-displaying the cells after a drag or -reloadData call. I'm not sure if this is just an issue for me, or for everyone, but I can confirm that it is very badly borked on my end.

Before:
2012-04-05 19:36:51.861 Application[54868:403] TUITableViewCell: 0x10059f820 at 0
2012-04-05 19:36:51.863 Application[54868:403] TUITableViewCell: 0x10057f520 at 1
2012-04-05 19:36:51.865 Application[54868:403] TUITableViewCell: 0x100572d70 at 2
2012-04-05 19:36:51.866 Application[54868:403] TUITableViewCell: 0x100595f50 at 3

After:
2012-04-05 19:37:02.715 Application[54868:403] TUITableViewCell: 0x100595f50 at 0
2012-04-05 19:37:02.717 Application[54868:403] TUITableViewCell: 0x100572d70 at 1
2012-04-05 19:37:02.718 Application[54868:403] TUITableViewCell: 0x10057f520 at 2
2012-04-05 19:37:02.720 Application[54868:403] TUITableViewCell: 0x10059f820 at 3

Please sign in to comment.
Something went wrong with that request. Please try again.