I am doing a news reader app and I want to make it so that the user can choose show/hide news categories (such as top news, business, technology, sports, etc) and reorder them like the BBC news app in Android.
See the picture below:
My question is:
(0) I assume you have your tables set up, &c.
(1) This solution only works if your tables cells are always draggable. Add this in your viewDidLoad to your Table View Controller .m file.
- (void)viewDidLoad
{
[super viewDidLoad];
[self setEditing:YES];
}
(2) To make it so you can reorder your cells, add cell.showsReorderControl = YES;
to your tableView:cellForRowAtIndexPath:.
(3) Ensure that you have the tableView:canMoveRowAtIndexPath: and tableView:moveRowAtIndexPath:toIndexPath: methods.
- (BOOL)tableView:(UITableView *)tableview canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
(4) Because you want the reorder control on the left, we have to take away the Delete circle usually there and the tableView:editingStyleForRowAtIndexPath: method does that.
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewCellEditingStyleNone;
}
(5) Last step, where the magic happens - add the tableView:willDisplayCell:forRowAtIndexPath: method, search for the cell's subviews, narrow it down to the private UITableViewCellReorderControl, and finally override it.
- (void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
for(UIView* view in cell.subviews)
{
if([[[view class] description] isEqualToString:@"UITableViewCellReorderControl"])
{
// Creates a new subview the size of the entire cell
UIView *movedReorderControl = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetMaxX(view.frame), CGRectGetMaxY(view.frame))];
// Adds the reorder control view to our new subview
[movedReorderControl addSubview:view];
// Adds our new subview to the cell
[cell addSubview:movedReorderControl];
// CGStuff to move it to the left
CGSize moveLeft = CGSizeMake(movedReorderControl.frame.size.width - view.frame.size.width, movedReorderControl.frame.size.height - view.frame.size.height);
CGAffineTransform transform = CGAffineTransformIdentity;
transform = CGAffineTransformTranslate(transform, -moveLeft.width, -moveLeft.height);
// Performs the transform
[movedReorderControl setTransform:transform];
}
}
}
I spent nearly ten hours trying to figure out a solution for when accessories and I couldn't do it. I need more experience and knowledge first. Simply doing the same thing to the reorder control on the disclosure button didn't work. So I hope this would works for now; and if I figure it out in the future I'll be sure to update this.
Hi Hoang Van Ha, this is my first answer ever, and I only started learning Objective-C a month ago, so I'm still quite a noob.
I've been trying to do the same thing with my app and have been searching for hours how to do it. Apple's documentation isn't very much help when it comes to this. I found it quite frustrating when people would simply link to the documentation. I wanted to yell at them. I hope my answer helped you.
Lots of kudos to b2Cloud because I used some of their code from this article and adapted it for my answers.
Instead of hunting for UITableViewCellReorderControl
, which is non-public API and might change in the future, I recommend using a UILongPressGestureRecognizer
over the UITableView
and implement your own reordering logic.
I solved the general case (responding to a long press on any part of the cell) in HPReorderTableView
, a drop-in replacement of UITableView. It can be easily modified to respond to touches on specific parts of the cell, for example by implementing gestureRecognizer:shouldReceiveTouch:
of the reorderGestureRecognizer
delegate.
Now there is a much easier solution for iOS 9 and later
tableView.semanticContentAttribute = .forceRightToLeft
Edit:
As Roland mentioned you should notice that "if you align your cells' subviews via leading
and trailing
anchors instead of left
and right
, this setting will flip everything."
Luke Dubert, have cool answer for iOS 7 it should be changed a little bit to:
UIView *cellSubview = cell.subviews[0];
for(UIView* view in cellSubview.subviews)
{
if([[[view class] description] isEqualToString:@"UITableViewCellReorderControl"])
{
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With