在UITableView中更改移动单元格的默认图标

dev*_*eek 35 uitableview ios

我需要在UITableView中更改移动单元格的默认图标.

这个:

在此输入图像描述

可能吗?

Ash*_*lls 58

这是一个真正的hacky解决方案,可能无法长期工作,但可能会给你一个起点.重新排序控件是a UITableViewCellReorderControl,但这是私有类,因此您无法直接访问它.但是,您可以查看子视图的层次结构并找到其imageView.

您可以通过子类化UITableViewCell和重写其setEditing:animated:方法来执行此操作,如下所示:

- (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"];
                    }
                }
            }
        }
    }   
}
Run Code Online (Sandbox Code Playgroud)

或者在斯威夫特

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")
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

但要注意......这可能不是一个长期解决方案,因为Apple可以随时更改视图层次结构.

  • 警告:这不适用于iOS 6.1+让我们说;) (4认同)
  • 这在iOS 8上也可以使用。因此,您可以在iOS 5、6和8上使用它,也可以在iOS 7上使用OgreSwamp的答案。唯一的区别是视图层次结构在iOS 7中更深一层。 (2认同)

Ric*_*gan 13

Ashley Mills的答案在提供时非常出色,但正如其他人在评论中所指出的那样,视图层次结构已经从iOS版本变为版本.为了正确地找到重新排序控件,我正在使用遍历整个视图层次结构的方法; 希望这将使该方法有机会继续工作,即使Apple更改视图层次结构.

这是我用来查找重新排序控件的代码:

-(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;
}
Run Code Online (Sandbox Code Playgroud)

这里是我用来覆盖-(void) setEditing:animated:我的子类中的方法的代码:

-(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);
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 太棒了,一个 3 岁的问题仍然得到答案!有一个+1! (2认同)

And*_*eev 7

Swick版本的Rick的答案几乎没有改进:

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
}
Run Code Online (Sandbox Code Playgroud)


Grz*_*sza 6

斯威夫特4

   // Change default icon (hamburger) for moving cells in UITableView
    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        let imageView = cell.subviews.first(where: { $0.description.contains("Reorder") })?.subviews.first(where: { $0 is UIImageView }) as? UIImageView

        imageView?.image = #imageLiteral(resourceName: "new_hamburger_icon") // give here your's new image
        imageView?.contentMode = .center

        imageView?.frame.size.width = cell.bounds.height
        imageView?.frame.size.height = cell.bounds.height
    }
Run Code Online (Sandbox Code Playgroud)

  • 这是2019年的解决方案 (2认同)

Ogr*_*amp 5

Ashley Mills的更新解决方案(适用于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"];
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


Hub*_*ang 5

我使用editingAccessoryView来替换重新排序图标.

  1. 创建UITableViewCell的子类.
  2. 覆盖setEditing.只需隐藏重新排序控件,并使用您的重新订购图像将editAccessoryView设置为uiimageview.
 - (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;

}
Run Code Online (Sandbox Code Playgroud)

如果您不使用编辑附件视图,这可能是一个不错的选择.

  • 虽然这可以显示自定义的editingAccessoryView,但我实际上无法通过按下并拖动单元格来移动单元格。 (2认同)