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

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

这个:

在这里输入图像说明

可能吗?

这是一个非常糟糕的解决scheme,可能不会长期工作,但可能会给你一个起点。 重新sorting控制是一个UITableViewCellReorderControl ,但这是一个私人类,所以你不能直接访问它。 但是,您可以通过子视图的层次结构来查看其imageView。

你可以通过setEditing:animated: 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"]; } } } } } } 

或者在Swift中

 override func setEditing(editing: Bool, animated: Bool) { super.setEditing(editing, animated: animated) if (editing) { for view in subviews as [UIView] { if view.dynamicType.description().rangeOfString("Reorder") != nil { for subview in view.subviews as [UIImageView] { if subview.isKindOfClass(UIImageView) { subview.image = UIImage(named: "yourimage.png") } } } } } } 

不过要警惕……这可能不是一个长期的解决scheme,因为苹果可以随时更改视图层次结构。

Ashley Mills的答案在提供时非常出色,但正如其他人在评论中指出的那样,iOS的版本之间的视图层次结构已经发生了变化。 为了正确地find重新sorting控件,我使用了遍历整个视图层次结构的方法; 希望这将使这个方法有机会继续工作,即使苹果改变了视图层次。

这里是我用来find重新sorting控制的代码:

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

以下是我用来覆盖子类中的-(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); } } } } } 

更新的Ashley Mills解决scheme(适用于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"]; } } } } } 

我使用editingAccessoryView来replace重新sorting图标。

  1. 做一个UITableViewCell的子类。
  2. 覆盖setEditing。 只需隐藏重新sorting控件,并将您的重新订购图像的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; } 

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

 - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { for (UIControl *control in cell.subviews) { if ([control isMemberOfClass:NSClassFromString(@"UITableViewCellReorderControl")] && [control.subviews count] > 0) { for (UIControl *someObj in control.subviews) { if ([someObj isMemberOfClass:[UIImageView class]]) { UIImage *img = [UIImage imageNamed:@"reorder_icon.png"]; ((UIImageView*)someObj).frame = CGRectMake(0.0, 0.0, 43.0, 43.0); ((UIImageView*)someObj).image = img; } } } } } 

瑞克答案的迅速版本,几乎没有改进:

 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 } 

您还可以简单地将您自己的自定义重新sorting视图添加到您的单元格内所有其他人。

所有你需要做的是确保这个自定义视图总是高于其他人,这可以在[UITableViewDelegate tableView: willDisplayCell: forRowAtIndexPath: indexPath:].

为了允许标准的重新sorting控制交互,您的自定义视图必须将其userInteractionEnabled设置为NO。

根据你的单元格的样子,你可能需要一个或多或less复杂的自定义重新sorting视图(模仿单元格背景为例)。