Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change default icon for moving cells in UITableView

I need to change default icon for moving cells in UITableView.

This one:

enter image description here

Is it possible?

like image 792
devgeek Avatar asked Dec 22 '11 11:12

devgeek


People also ask

Which method is used to allow moving of row in UITableView?

moveRow(at:to:) Moves the row at a specified location to a destination location.

How do I add a prototype cell to UITableView?

First, design your table cell on Storyboard whatever you want. Now, make a subclass of UITableViewCell and assign this class to your prototype custom cell which you have designed on Storyboard. Also, don't forget to set "reuse identifier in Storyboard table cell.


2 Answers

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.

like image 174
Ashley Mills Avatar answered Sep 20 '22 16:09

Ashley Mills


Ashley Mills' answer was excellent at the time it was offered, but as others have noted in the comments, the view hierarchy has changed from version to version of iOS. In order to properly find the reorder control, I'm using an approach that traverses the entire view hierarchy; hopefully this will give the approach an opportunity to continue working even if Apple changes the view hierarchy.

Here's the code I'm using to find the reorder control:

-(UIView *) findReorderView:(UIView *) view {     UIView *reorderView = nil;     for (UIView *subview in view.subviews)     {         if ([[[subview class] description] rangeOfString:@"Reorder"].location != NSNotFound)         {             reorderView = subview;             break;         }         else         {             reorderView = [self findReorderView:subview];             if (reorderView != nil)             {                 break;             }         }     }     return reorderView; } 

And here's the code I'm using to override the -(void) setEditing:animated: method in my subclass:

-(void) setEditing:(BOOL)editing animated:(BOOL)animated {     [super setEditing:editing animated:animated];     if (editing)     {         // I'm assuming the findReorderView method noted above is either         // in the code for your subclassed UITableViewCell, or defined         // in a category for UIView somewhere         UIView *reorderView = [self findReorderView:self];         if (reorderView)         {             // I'm setting the background color of the control             // to match my cell's background color             // you might need to do this if you override the             // default background color for the cell             reorderView.backgroundColor = self.contentView.backgroundColor;             for (UIView *sv in reorderView.subviews)             {                 // now we find the UIImageView for the reorder control                 if ([sv isKindOfClass:[UIImageView class]])                 {                     // and replace it with the image we want                     ((UIImageView *)sv).image = [UIImage imageNamed:@"yourImage.png"];                     // note:  I have had to manually change the image's frame                     // size to get it to display correctly                     // also, for me the origin of the frame doesn't seem to                     // matter, because the reorder control will center it                     sv.frame = CGRectMake(0, 0, 48.0, 48.0);                 }             }         }     } } 
like image 39
Rick Morgan Avatar answered Sep 20 '22 16:09

Rick Morgan