UITableViewRowAction title as image icon instead of text

前端 未结 8 1927
谎友^
谎友^ 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:13

    If you use answer of "Apps Wise"(https://stackoverflow.com/a/32735211/6249148), you can use this table for common unicode characters: https://tutorialzine.com/2014/12/you-dont-need-icons-here-are-100-unicode-symbols-that-you-can-use

    Or this one for ALL unicode characters: https://unicode-table.com/en/#control-character

    0 讨论(0)
  • 2020-12-07 23:14

    Try this

    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]
    }
    

    0 讨论(0)
  • 2020-12-07 23:23

    In iOS 11, Apple provides a way to help us do that. There are 2 functions you need to implement to swipe cell to left or right

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

    For example:

    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])
        }
    

    If you want to apply it for iOS < 11.0, you can do it by a tricky way

    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]
        }
    
    0 讨论(0)
  • 2020-12-07 23:28

    I was looking to do the same thing and found an answer here. It is a bit of a workaround.

    Also, as seen from the iOS 9 betas, I think this will be possible, but I have not yet looked at the APIs.

    0 讨论(0)
  • 2020-12-07 23:32

    There are two ways you can achieve this.

    • Use Unicode characters in your text, if it serves your purpose, However unicode characters are limited set.
    • Use emojis provided that it serve your purpose.

    This is how you do it in the code

     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]
    }
    

    This is what I could achieve by above mentioned ways -

    0 讨论(0)
  • 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\]
            }
    

    0 讨论(0)
提交回复
热议问题