Change default icon for moving cells in UITableView

前端 未结 11 1457
面向向阳花
面向向阳花 2020-11-28 21:46

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

This one:

\"enter

相关标签:
11条回答
  • 2020-11-28 22:00

    You can also simply add your own custom reorder view above all others inside your cell.

    All you have to do is ensure this custom view is always above others, which can be checked in [UITableViewDelegate tableView: willDisplayCell: forRowAtIndexPath: indexPath:].

    In order to allow the standard reorder control interaction, your custom view must have its userInteractionEnabled set to NO.

    Depending on how your cell looks like, you might need a more or less complex custom reorder view (to mimic the cell background for exemple).

    0 讨论(0)
  • 2020-11-28 22:05

    Updated solution of Ashley Mills (for iOS 7.x)

    if (editing) {
        UIView *scrollView = self.subviews[0];
        for (UIView * view in scrollView.subviews) {
            NSLog(@"Class: %@", NSStringFromClass([view class]));
            if ([NSStringFromClass([view class]) rangeOfString: @"Reorder"].location != NSNotFound) {
                for (UIView * subview in view.subviews) {
                    if ([subview isKindOfClass: [UIImageView class]]) {
                        ((UIImageView *)subview).image = [UIImage imageNamed: @"moveCellIcon"];
                    }
                }
            }
        }
    }
    
    0 讨论(0)
  • 2020-11-28 22:05

    After debuging the UITableViewCell, you can use KVC in UITableViewCell subclass to change it.

    // key
    static NSString * const kReorderControlImageKey = @"reorderControlImage";
    
    // setting when cellForRow calling
    UIImage *customImage;
    [self setValue:customImage forKeyPath:kReorderControlImageKey];
    
    // to prevent crash
    - (void)setValue:(id)value forUndefinedKey:(NSString *)key {
        if ([key isEqualToString:kReorderControlImageKey]) return;
        else [super setValue:value forUndefinedKey:key];
    }
    
    0 讨论(0)
  • 2020-11-28 22:09

    Swift version of Rick's answer with few improvements:

    override func setEditing(editing: Bool, animated: Bool) {
        super.setEditing(editing, animated: animated)
    
        if editing {
            if let reorderView = findReorderViewInView(self), 
                imageView = reorderView.subviews.filter({ $0 is UIImageView }).first as? UIImageView {
                imageView.image = UIImage(named: "yourImage")
            }
        }
    }
    
    func findReorderViewInView(view: UIView) -> UIView? {
        for subview in view.subviews {
            if String(subview).rangeOfString("Reorder") != nil {
                return subview
            }
            else {
                findReorderViewInView(subview)
            }
        }
        return nil
    }
    
    0 讨论(0)
  • 2020-11-28 22:15

    I use editingAccessoryView to replace reorder icon.

    1. Make a subclass of UITableViewCell.
    2. Override setEditing. Simply hide reorder control and set editingAccessoryView to an uiimageview with your re-order image.
     - (void) setEditing:(BOOL)editing animated:(BOOL)animated
    {
    
        [super setEditing: editing animated: YES];
    
        self.showsReorderControl = NO;
    
        self.editingAccessoryView = editing ? [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"yourReorderIcon"]] : nil;
    
    }
    

    If you are not using editing accessory view, this may be a good choice.

    0 讨论(0)
  • 2020-11-28 22:20

    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);
                    }
                }
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题