如何在UITableViewRowAction中添加图像?

Jac*_*ack 11 uitableview ios swift

我正在尝试以UITableView滑动样式添加图像.我尝试使用Emoji文本及其工作正常

func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
  let editAction = UITableViewRowAction(style: .normal, title: "") { (rowAction, indexPath) in
      print("edit clicked")
  }

  return [editAction]
}
Run Code Online (Sandbox Code Playgroud)

但我需要图像而不是表情符号,同时我试过

editAction.backgroundColor = UIColor.init(patternImage: UIImage(named: "edit")!)
Run Code Online (Sandbox Code Playgroud)

但它正在获得重复的图像,我使用了许多格式的图像,如20*20,25*25,50*50,但仍然重复.

我该如何添加图片?

Jac*_*ack 21

最后在iOS 11中,SWIFT 4我们可以借助于UITableView的滑动动作添加添加图像UISwipeActionsConfiguration

@available(iOS 11.0, *)
    func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {

            let action =  UIContextualAction(style: .normal, title: "Files", handler: { (action,view,completionHandler ) in
                //do stuff
                completionHandler(true)
            })
        action.image = UIImage(named: "apple.png")
        action.backgroundColor = .red
        let confrigation = UISwipeActionsConfiguration(actions: [action])

        return confrigation
    }
Run Code Online (Sandbox Code Playgroud)

WWDC视频,电话28.34

Apple Doc

注意:我使用50*50点apple.png图像50 tableview row height


Isu*_*ssa 5

我的项目也遇到了同样的问题,所以我为此做了一个解决方法。我认为这对您有帮助。

当我仅向左滑动表格单元格以获得图像宽度时,它工作正常。

在此处输入图片说明

但是,当我滑动表格单元格超过图像宽度时,表格单元格显示如下:

在此处输入图片说明

发生这种情况是因为要添加图像,我使用了“ backgroundColor”属性。

copyButton.backgroundColor = UIColor(patternImage: UIImage(named: "bfaCopyIcon.png")!)
Run Code Online (Sandbox Code Playgroud)

因此,要解决此问题,我将图像宽度增加到与表格宽度相同。

旧图片 >>>>>>>>>>>> 新图片

在此处输入图片说明 >>>> 在此处输入图片说明

这是新外观:

在此处输入图片说明

这是我的示例代码:

func tableView(_ tableView: UITableView, editActionsForRowAt: IndexPath) -> [UITableViewRowAction]? {
    let copyButton = UITableViewRowAction(style: .normal, title: "") { action, index in

         print("copy button tapped")

    }
    copyButton.backgroundColor = UIColor(patternImage: UIImage(named: "bfaCopyIcon.png")!)


    let accessButton = UITableViewRowAction(style: .normal, title: "") { action, index in

       print("Access button tapped")

    }
    accessButton.backgroundColor = UIColor(patternImage: UIImage(named: "bfaAccess.png")!)


    return [accessButton, copyButton]
}
Run Code Online (Sandbox Code Playgroud)