我最近遇到了更改重新排序控件的图像的需要,因为我将UITableViewCell 子类化以提供我自己的自定义表格单元格。作为这项工作的一部分,我将单元格的背景更改为默认颜色以外的其他颜色。
一切正常,但是当我将UITableView 置于编辑模式时,将出现重新排序控件,其背景为白色 - 而不是我用于单元格的背景颜色。这看起来不太好,我希望背景匹配。
在 iOS 的各个版本的过程中,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;
}
在您的自定义 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);
}
}
}
}
}
您的里程可能会有所不同;我希望这对你有用。