How to Make Reorder Control of Uitableviewcell in Left Side

How do I make reorder control of UITableViewCell in left side?

Answer [If you're not using any accessories (detail disclosure, &c)]

(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.

Having the Reorder Symbol on the Left Side in a UITableViewCell

You keep moving the reorder control relative to its current position

CGSize moveLeft = CGSizeMake(movedReorderControl.frame.size.width - view.frame.size.width, movedReorderControl.frame.size.height - view.frame.size.height);

so it seems like its moving it off screen. Try storing the initial frame and then using that to shift your subview:

CGSize moveLeft = CGSizeMake(initialFrame.width - view.frame.size.width, initialFrame.width.height - view.frame.size.height);

Moving reordering control to left

Try this, I hope it helps

- (void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell
forRowAtIndexPath:(NSIndexPath *)indexPath
{
for(UIView* view in cell.subviews)
{
if([[[view class] description] isEqualToString:@"UITableViewCellReorderControl"])
{

UIView* resizedGripView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetMaxX(view.frame), CGRectGetMaxY(view.frame))];
[resizedGripView addSubview:view];
[cell addSubview:resizedGripView];

// Original transform
const CGAffineTransform transform = CGAffineTransformMakeTranslation(view.frame.size.width - cell.frame.size.width, 1);
// Move custom view so the grip's top left aligns with the cell's top left

[resizedGripView setTransform:transform];
}
}
}

UITableViewCell reorder control, no delete button, and swipe-to-delete

To do this I combined two open source projects into:

https://github.com/adamraudonis/UITableViewCell-Swipe-for-Options

Hope that helps! Currently you can reorder by long-pressing anywhere in the TableViewCell and swipe to delete without showing the red circle.

How to change UITableViewCell indentation direction

We can do this by defining semanticContentAttribute property. See https://stackoverflow.com/a/44254401/2716479

UITableViewCell - How to get a pointer to the reorder control?

You need to use the editingAccessoryView property on your cell :

cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
UIView *editingAccessory = cell.editingAccessoryView;

UITableViewCell custom reorder control

I put a little work into this recently, but came up short. I tried setting my own editingAccesoryView but couldn't get the reorder control to change. Odd.

My guess is that it has something to do with the following comment in the UITableviewCell docs re: showsReorderControl:

If the value is YES , the reordering
control temporarily replaces any
accessory view.

In other words, the editingAccessoryView is being replaced by the reordering control view, which might be why we cannot override the reordering control. Hoping someone can find a workaround.



Related Topics



Leave a reply



Submit