标题的 UITableViewRowAction 图像 [英] UITableViewRowAction image for title

查看:27
本文介绍了标题的 UITableViewRowAction 图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了一个自定义的 UITableViewRowAction.现在我想添加一个图像而不是文本.我知道这是可能的,但不知道如何去做.你们中有人知道如何在 Swift 中执行此操作并愿意帮助我吗?感谢您的回答!

I made a custom UITableViewRowAction. Now I'd like to add an image instead of the text. I know that it's possible but don't know how to do it. Does someone of you knows how to do this in Swift and would like to help me? Thanks for your answers!

推荐答案

iOS 11.0

迅捷

Apple 引入了灵活的方式来声明行操作,并带来巨大好处.

iOS 11.0

Swift

Apple introduced flexible way to declare row actions with great benefits.

extension ViewController: UITableViewDelegate {
  func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
    let askAction = UIContextualAction(style: .normal, title: nil) { action, view, complete in
      print("Ask!")
      complete(true)
    }

    // here set your image and background color
    askAction.image = IMAGE
    askAction.backgroundColor = .darkGray

    let blockAction = UIContextualAction(style: .destructive, title: "Block") { action, view, complete in
      print("Block")
      complete(true)
    }

    return UISwipeActionsConfiguration(actions: [blockAction, askAction])
  }

  func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    cell.textLabel?.text = "row: (indexPath.row)"
  }
}

示例:

需要将UIImage设置为row action的backgroundColor,具体通过:

You need to set UIImage to backgroundColor of row action, concretely by:

UIColor(patternImage: UIImage(named: "IMAGE_NAME"))

目标-C:

[UIColor colorWithPatternImage:[UIImage imageNamed:@"IMAGE_NAME"]];

这篇关于标题的 UITableViewRowAction 图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆