UITableViewCell自定义重新排序控件

Rap*_*ert 12 iphone cocoa-touch uitableview ios4

有没有办法更改UITableView处于编辑模式时显示的重新排序控件的图像?我有一个UIImage,我想显示而不是通常的灰色条.

我是否必须继承UITableViewCell才能完成此任务?

Ash*_*lls 9

我想你现在还有很长的路要走,但这已经出现在一个新的问题中了.

在这里看到我的答案:

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


Ric*_*gan 5

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

一切正常,但是当我将其UITableView置于编辑模式时,会出现重新排序控件,背景为白色 - 而不是我用于单元格的背景颜色。这看起来不太好,我希望背景匹配。

在各种版本的 iOS 过程中,a 中的视图层次结构发生了UITableViewCell变化。我采用了一种方法,它会遍历整个视图集,直到找到UITableViewCellReorderControl私有类。我相信这将适用于本回答时的 iOS 5 和所有后续 iOS 版本。请注意,虽然UITableViewCellReorderControl类本身是私有的,但我没有使用任何私有 API 来查找它。

首先,这是扫描重新排序控件的代码;我假设文本“重新排序”将在类名中 - 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)

在您的自定义 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);
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

你的旅费可能会改变; 我希望这对你有用。


And*_*tle 1

我最近为此做了一些工作,但效果不佳。我尝试设置自己的EditingAccesoryView,但无法更改重新排序控件。奇怪的。

我的猜测是,它与 UITableviewCell 文档中的以下评论有关:showsReorderControl

如果值为 YES ,则重新排序控件会暂时替换任何附件视图。

换句话说,editingAccessoryView 正在被重新排序控件视图取代,这可能就是我们无法覆盖重新排序控件的原因。希望有人能找到解决方法。