I want to allow the default row movement in a UITableView
without it being in editing mode, and without compromising the default behaviour of the UITableView
.
The image above displays a cell in editing mode, with movement enabled.
I tried simply running for (UIView *subview in cell.subviews)
(while my UITableView
was in editing mode), but the button didn't turn up:
<UITableViewCellScrollView: 0x8cabd80; frame = (0 0; 320 44); autoresize = W+H; gestureRecognizers = <NSArray: 0x8c9ba20>; layer = <CALayer: 0x8ca14b0>; contentOffset: {0, 0}>
How can allow/add the movement "button" without enabling editing mode in my UITableView
?
Creating and adding a UIButton
with the default function
for movement is also an option.
I actually do something similar for one of my apps. It uses the delegate methods for table editing and a bit of 'tricking' the user. 100% built-in Apple functionality.
1 - Set the table to editing (I do it in viewWillAppear)
2 - Hide the default accessory icon:
3 - Keep editing mode from moving everything to the right (in the cell)
4 - At this point you should be able to drag the cells around without it looking like it is in editing mode. Here we trick the user. Create your own "move" icon (three lines in the default case, whatever icon you want in your case) and add the imageView right where it would normally go on the cell.
5 - Finally, implement the delegate method to actually rearrange your underlying datasource.
William Falcon's answer in swift 3
1 - Set the table to editing (I do it in viewWillAppear)
2 - Hide the default accessory icon:
3 - Keep editing mode from moving everything to the right (in the cell)
4 - Not required in swift 3
5 - Reorder your array
Extra Note
If you want to have your table cells selectable, add the following code within
viewWillAppear()
function.If you don't want to set the
UITableView
into editing mode then you will need to reinvent the capability for dragging cells around.I present a fairly complete solution that enables the user to move rows around within the visible area of the
UITableView
. It works whether theUITableView
is in editing mode or not. You'll need to extend it if you want the table to scroll when a row is dragged near the top or bottom of the visible area. There are likely some failure and edge cases you'll need to ferret out as well.