Change Default Icon for Moving Cells in Uitableview

Change default icon for moving cells in UITableView

This is a really hacky solution, and may not work long term, but may give you a starting point. The re-order control is a UITableViewCellReorderControl, but that's a private class, so you can't access it directly. However, you could just look through the hierarchy of subviews and find its imageView.

You can do this by subclassing UITableViewCell and overriding its setEditing:animated: method as follows:

- (void) setEditing:(BOOL)editing animated:(BOOL)animated
{
[super setEditing: editing animated: YES];

if (editing) {

for (UIView * view in self.subviews) {
if ([NSStringFromClass([view class]) rangeOfString: @"Reorder"].location != NSNotFound) {
for (UIView * subview in view.subviews) {
if ([subview isKindOfClass: [UIImageView class]]) {
((UIImageView *)subview).image = [UIImage imageNamed: @"yourimage.png"];
}
}
}
}
}
}

Or in Swift

override func setEditing(_ editing: Bool, animated: Bool) {
super.setEditing(editing, animated: animated)

if editing {
for view in subviews where view.description.contains("Reorder") {
for case let subview as UIImageView in view.subviews {
subview.image = UIImage(named: "yourimage.png")
}
}
}
}

Be warned though... this may not be a long term solution, as Apple could change the view hierarchy at any time.

Modify reorder control icon in table view editing mode

Glad we're getting your issue sorted. For your final issue, I'd suggest that in cellForRowAtIndexPath: that you explicitly call the replacement logic to force your code to be called each time a cell is displayed.

(As an aside, logging is always a great way to figure out a puzzling issue,)

How to remove the default Edit Icons in each cell in Xamarin iOS

It is almost impossible. Here is a hacky solution. I converted it from oc to C#.

public override void SetEditing(bool editing, bool animated)
{
base.SetEditing(editing, animated);

if (editing)
{
foreach(var view in this.Subviews)
{
if(view.Class.Name is "Reorder")
{
foreach (var subview in view.Subviews)
{
((UIImageView)subview).Image = UIImage.FromFile("yourimage.png");
}
}
}
}
}

However, the solution may be broken at any time when Apple changes the hierarchy of the cell, so I don't suggest you use this way.

How to change left icon in UITableView editing mode

You cannot change the (-) icon, however, you can implement your own editing style.
Check UITableView's setEditing:animated: method where you can make your editing style on the cell

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.

Change color of Reorder icon in UITableView

You can set a custom image you like by giving this code within your UITableViewCell class.

override func setEditing(_ editing: Bool, animated: Bool) {
super.setEditing(editing, animated: animated)

if editing {
for view in subviews where view.description.contains("Reorder") {
for case let subview as UIImageView in view.subviews {
subview.image = UIImage(named: "yourimage.png")?.withRenderingMode(.alwaysTemplate)
subview.tintColor = .red // your desired color here
}
}
}
}

How to set Xamarin iOS UITableView Reorder icon color

Inside the UITableViewCell whose reorder icon color you wish to change. Override SetEditing with the following code.

public override void SetEditing(bool editing, bool animated)
{
base.SetEditing(editing, animated);
if (editing)
{
foreach (var subViewA in Subviews)
{
if (subViewA.IsMemberOfClass(new ObjCRuntime.Class("UITableViewCellReorderControl")))
{
foreach (var subViewB in subViewA.Subviews)
{
if (subViewB is UIImageView imageView)
{
imageView.Image = imageView.Image = imageView.Image.ImageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate);
imageView.TintColor = UIColor.White;
}
}
}
}
}
}


Related Topics



Leave a reply



Submit