代码之家  ›  专栏  ›  技术社区  ›  Raphael Schweikert miya

UITableViewCell自定义重新排序控件

  •  12
  • Raphael Schweikert miya  · 技术社区  · 14 年前

    当UITableView处于编辑模式时,是否有任何方法可以更改显示的重新排序控件的图像?我有一个ui图像,我想显示而不是通常的灰色条。

    是否必须对UITableViewCell进行子类化才能完成此操作?

    5 回复  |  直到 8 年前
        1
  •  1
  •   iwasrobbed    12 年前

    我最近对此做了一点工作,但结果很差。我尝试设置自己的editingAccesoryView,但无法更改重新排序控件。奇怪的。

    我想这与uiTableViewCell文档中的以下注释有关: 显示顺序控制 :

    如果值为“是”,则重新排序 控件临时替换任何 附件视图。

    换句话说,editingaccessoryView正被重新排序控件视图替换,这可能是我们无法重写重新排序控件的原因。希望有人能找到解决办法。

        2
  •  9
  •   Community Lee    7 年前

    我想你已经过了很长一段时间了,但这是一个新的问题。

    请看我的答案:

    Change default icon for moving cells in UITableView

        3
  •  4
  •   Rick Morgan    10 年前

    我最近遇到了更改重排序控件图像的需求,因为我将 UITableViewCell 提供我自己的自定义表格单元格。作为这项工作的一部分,我将单元格的背景更改为默认颜色以外的其他颜色。

    一切正常,但当我把 UITableView 在编辑模式下,重新排序控件将显示为白色背景,而不是我用于单元格的背景色。这看起来不太好,我想让背景匹配。

    在各种版本的iOS过程中,视图的层次结构 UITababVIEW单元格 改变了。我已经采取了一种方法,它将遍历整个视图集,直到找到 UITableViewCellReorderControl 私人班级。我相信这将适用于iOS 5和所有后续的iOS版本在这个答案的时候。请注意,当 UITableViewCellReorderControl 类本身是私有的,我没有使用任何私有API来查找它。

    首先,这里是用于扫描重新排序控件的代码;我假设文本“reorder”将位于类名中—苹果将来可能会更改它:

    -(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;
    }
    

    在自定义的UITableViewCell子类中,将重写 -(void) setEditing:animated: 在这里找到重新排序控件。如果在表未处于编辑模式时尝试查找此控件,则重新排序控件将不在单元格的视图层次结构中:

    -(void) setEditing:(BOOL)editing animated:(BOOL)animated
    {
        [super setEditing:editing animated:animated];
        if (editing)
        {
            // find the reorder view here
            // place the previous method either directly in your
            // subclassed UITableViewCell, or in a category
            // defined on UIView
            UIView *reorderView = [self findReorderView:self];
            if (reorderView)
            {
                // here, I am changing the background color to match my custom cell
                // you may not want or need to do this
                reorderView.backgroundColor = self.contentView.backgroundColor;
                // now scan the reorder control's subviews for the reorder image
                for (UIView *sv in reorderView.subviews)
                {
                    if ([sv isKindOfClass:[UIImageView class]])
                    {
                        // and replace the image with one that you want
                        ((UIImageView *)sv).image = [UIImage imageNamed:@"yourImage.png"];
                        // it may be necessary to properly size the image's frame
                        // for your new image - in my experience, this was necessary
                        // the upper left position of the UIImageView's frame
                        // does not seem to matter - the parent reorder control
                        // will center it properly for you
                        sv.frame = CGRectMake(0, 0, 48.0, 48.0);
                    }
                }
            }
        }
    }
    

    你的里程可能会有所不同,我希望这对你有用。

        4
  •  0
  •   Clifton Labrum    9 年前

    也许我们都想得太多了。:)

    只需定制 UIImageView 在默认移动附件的顶部,这样它就可以将其覆盖起来。完成。

        5
  •  -1
  •   TechZen    14 年前

    你设置了手机的 editingAccessoryView 属性设置为包含所需图像的图像视图。

    作为旁白,我会提醒你在做这件事时要小心。当您将自定义图形替换为系统标准(如重新排序图形)时,将面临使用户混淆的严重风险。标准的用户界面语法告诉他们在重新排序时需要标准图形,他们可能不理解自定义图形的意义。