UITableViewRowAction title as image icon instead of text

前端 未结 8 1926
谎友^
谎友^ 2020-12-07 22:53

I want put the icon (image) instead of text in swipe actions in tableviewCell in Swift.

But i dont how to put the image instead of title text?

My code is be

8条回答
  •  有刺的猬
    2020-12-07 23:33

    Check below working code

    func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> \[UITableViewRowAction\]? {
    
    
                let backView = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: tableView.frame.size.height))
                backView.backgroundColor = UIColor(red: 239/255.0, green: 34/255.0, blue: 91/255.0, alpha: 1.0)
    
                let frame = tableView.rectForRow(at: indexPath)
    
    
                let myImage = UIImageView(frame: CGRect(x: 20, y: frame.size.height/2-20, width: 35, height: 35))
                myImage.image = UIImage(named: "delete_white")!
                backView.addSubview(myImage)
    
                let imgSize: CGSize = tableView.frame.size
                UIGraphicsBeginImageContextWithOptions(imgSize, false, UIScreen.main.scale)
                let context = UIGraphicsGetCurrentContext()
                backView.layer.render(in: context!)
                let newImage: UIImage = UIGraphicsGetImageFromCurrentImageContext()!
                UIGraphicsEndImageContext()
    
                let deleteAction = UITableViewRowAction(style: .destructive, title: "           ") {
                    (action, indexPath) in
                    self.deleteCartItem(indexPath: indexPath )
                    self.addWebtrendsForCart(path: "/Basket/EditBasket", description: "Edit Basket")
                }
    
                 deleteAction.backgroundColor = UIColor(patternImage: newImage)
                 return \[deleteAction\]
            }
    

提交回复
热议问题