UITableViewRowAction标题作为图像图标而不是文本

我想把图标(图像),而不是文本Swift中的tableviewCell中的滑动操作。

但我不怎么把图像,而不是标题文字?

我的代码如下:

func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [AnyObject]? { var shareAction = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: "Share" , handler: { (action:UITableViewRowAction!, indexPath:NSIndexPath!) -> Void in // 2 let shareMenu = UIAlertController(title: nil, message: "Share using", preferredStyle: .ActionSheet) let twitterAction = UIAlertAction(title: "Twitter", style: UIAlertActionStyle.Default, handler: nil) let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: nil) shareMenu.addAction(twitterAction) shareMenu.addAction(cancelAction) self.presentViewController(shareMenu, animated: true, completion: nil) }) // 3 var rateAction = (style: UITableViewRowActionStyle.Default, title: "rate",im , handler: { (action:UITableViewRowAction!, indexPath:NSIndexPath!) -> Void in // 4 let rateMenu = UIAlertController(title: nil, message: "Rate this App", preferredStyle: .ActionSheet) let appRateAction = UIAlertAction(title: "Rate", style: UIAlertActionStyle.Default, handler: nil) let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: nil) rateMenu.addAction(appRateAction) rateMenu.addAction(cancelAction) self.presentViewController(rateMenu, animated: true, completion: nil) }) // 5 return [shareAction,rateAction] } 

你能帮我一下吗?

提前致谢

有两种方法可以实现这一点。

  • 在你的文本中使用Unicode字符,如果它符合你的目的,但Unicode字符是有限的设置。
  • 只要符合您的目的,使用emojis。

这是你如何在代码中做到这一点

  override func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? { let delete = UITableViewRowAction(style: .Default, title: "\u{267A}\n Delete") { action, index in print("more button tapped") self.tableView(tableView, commitEditingStyle: UITableViewCellEditingStyle.Delete, forRowAtIndexPath: indexPath) } delete.backgroundColor = UIColor(rgba: "#ef3340") let apply = UITableViewRowAction(style: .Default, title: "\u{2606}\n Like") { action, index in print("favorite button tapped") self.tableView(tableView, commitEditingStyle: UITableViewCellEditingStyle.Insert, forRowAtIndexPath: indexPath) } apply.backgroundColor = UIColor.orangeColor() let take = UITableViewRowAction(style: .Normal, title: "\u{2605}\n Rate") { action, index in print("share button tapped") self.tableView(tableView, commitEditingStyle: UITableViewCellEditingStyle.None, forRowAtIndexPath: indexPath) } take.backgroundColor = UIColor(rgba: "#00ab84") return [take, apply, delete] } 

这是我可以通过上述方式实现的 –

在这里输入图像说明

iOS 11中 ,Apple提供了一种方法来帮助我们做到这一点。 有两个function需要执行,以将单元格向左或向右滑动

 public func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? public func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? 

例如:

 func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? { let deleteAction = UIContextualAction(style: .normal, title: "", handler: { (ac:UIContextualAction, view:UIView, success:(Bool) -> Void) in // Call edit action // Reset state success(true) }) deleteAction.image = UIImage(named: "ic_delete") deleteAction.backgroundColor = .red return UISwipeActionsConfiguration(actions: [deleteAction]) } 

如果你想把它应用于iOS <11.0 ,你可以通过一个棘手的方式来做到这一点

 func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? { let deleteAction = UITableViewRowAction(style: .default, title: "") { action, indexPath in // Handle delete action } (UIButton.appearance(whenContainedInInstancesOf: [UIView.self])).setImage(UIImage(named: "ic_delete"), for: .normal) return [deleteAction] } 

不是Swift,但是对于任何其他在这里寻找解决scheme的人来说,我用以下简单的子类得到了很好的结果:

 @interface GSBTableViewRowAction : UITableViewRowAction @property UIImage *icon; @property UIFont *font; + (instancetype)rowActionWithStyle:(UITableViewRowActionStyle)style title:(NSString *)title icon:(UIImage*)icon handler:(void (^)(UITableViewRowAction *action, NSIndexPath *indexPath))handler; @end @implementation GSBTableViewRowAction + (instancetype)rowActionWithStyle:(UITableViewRowActionStyle)style title:(NSString *)title icon:(UIImage*)icon handler:(void (^)(UITableViewRowAction *action, NSIndexPath *indexPath))handler { if (title.length) title = [@"\n" stringByAppendingString:title]; // move title under centerline; icon will go above GSBTableViewRowAction *action = [super rowActionWithStyle:style title:title handler:handler]; action.icon = icon; return action; } - (void)_setButton:(UIButton*)button { if (self.font) button.titleLabel.font = self.font; if (self.icon) { [button setImage:[self.icon imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate] forState:UIControlStateNormal]; button.tintColor = button.titleLabel.textColor; CGSize titleSize = [button.titleLabel.text sizeWithAttributes:@{NSFontAttributeName:button.titleLabel.font}]; button.imageEdgeInsets = UIEdgeInsetsMake(-(titleSize.height/2 + 5), 0, 0, -titleSize.width); // +5px gap under icon } 

rowActionWithStyle:title:icon:handler实际上只是一个方便的方法 – '秘密'就是覆盖(private?)_setButton方法[相信Jayesh!]。

这将给你一个标题集中的图标,或者只有图标,如果你离开标题零。 不幸的是,你不能摆弄标题的位置使用button.titleEdgeInsets ,就像你可以imageEdgeInsets ,所以我可以做的最好的是把标题立即在中心线下(因此'\ N')与图标下的小差距5px以上)。 结果看起来像

行标题+图标

作为奖励,您还可以通过设置新的font属性来更改标题字体,以表示更小的font 。 例如上面的“添加”动作是用完成的

 GSBTableViewRowAction *add = [GSBTableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"Add" icon:[UIImage imageNamed:@"Today-25"] handler:^(UITableViewRowAction *action, NSIndexPath *indexPath){ [self addToCalendar:self]; [tableView setEditing:NO animated:YES]; }]; add.font = [UIFont preferredFontForTextStyle:UIFontTextStyleFootnote]; add.backgroundColor = UIColor.orangeColor; 

警告:_setButton是私人API,所以YMMV …我们都希望苹果公开API来做他们在他们的Mail.app [原文如此]

尝试这个

 func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? { let remove = UITableViewRowAction(style: .Default, title: " ") { action, indexPath in print("delete button tapped") } remove.backgroundColor = UIColor(patternImage: UIImage(named: "Delete")!) return [remove] } 

在这里输入图像说明

我期待着做同样的事情,并在这里find答案。 这是一个解决方法。

另外,从iOS 9 Beta版中可以看出,我认为这将是可能的,但是我还没有看过API。

trailingSwipeActionsConfigurationForRowAtIndexPath仅适用于iOS 11,完全不可定制。 您可以更改button的背景颜色,但即使使用UIImageRenderingModeAlwaysOriginal ,也不能添加具有自己颜色的图像。

我结束了使用MGSwipeTableCell

如果您使用“Apps Wise”( https://stackoverflow.com/a/32735211/6249148 )的答案,则可以使用此表查找常见的unicode字符: https : //tutorialzine.com/2014/12/you-dont -需要-图标-在这里,是-100-Unicode的符号,但─友可以使用

或者这一个为所有的Unicode字符: https : //unicode-table.com/en/#control-character