简体   繁体   English

tableView:editActionsForRowAt:向右滑动?

[英]tableView:editActionsForRowAt: on right-swipe?

As I was using tableView:editActionsForRowAt: to provide some useful functionalities in an iOS app. 当我使用tableView:editActionsForRowAt:在iOS应用程序中提供一些有用的功能时。

func tableView(_ tableView: UITableView,
               editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
    print(#function)
    .... useful code .....
}

I got to the point where some other API similar to the one above, but fired on a right swipe, rather than left swipe like tableView:editActionsForRowAt: would have been useful. 我已经说到了其他一些与上述API类似的API,但它是向右滑动而不是像tableView:editActionsForRowAt:那样向左滑动触发的 Say something like: 像这样说:

func tableView(_ tableView: UITableView,
               editMoreActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
    print(#function)
    .... useful code .....
}

Browsing the net I found those two methods of the UITableViewDelegate protocol: 浏览网络时,我发现了UITableViewDelegate协议的以下两种方法:

tableView:leadingSwipeActionsConfigurationForRowAt
tableView:trailingSwipeActionsConfigurationForRowAt

After some basic experimentation, I thought I could get what I wanted, using tableView:leadingSwipeActionsConfigurationForRowAt , so providing some more functionalities to the user on the right swipe of a cell. 经过一些基本的实验之后,我认为我可以使用tableView:leadingSwipeActionsConfigurationForRowAt来获得想要的功能,从而在向右滑动单元格时向用户提供更多功能。 But it seems like this method doesn't give much freedom. 但是,似乎这种方法并没有提供太多的自由。 For example, here the code I have (very much inspired by some sample I found on the internet): 例如,这里有我的代码(非常启发我在互联网上找到的一些示例):

func tableView(_ tableView: UITableView,
               leadingSwipeActionsConfigurationForRowAt 
               indexPath: IndexPath) -> UISwipeActionsConfiguration? {
    let modifyAction = UIContextualAction(style: .normal, title: nil, handler: {
        (ac:UIContextualAction, view:UIView, success:(Bool) -> Void) in
        print("Update action ...")
        success(true)
    })

    modifyAction.image = UIImage(named: "MyNiceIcon")!.withColor(color: .black)
    modifyAction.backgroundColor = .clear

    return UISwipeActionsConfiguration(actions: [modifyAction])
}

Here even though these lines are present: 即使存在以下几行,也可以在这里:

modifyAction.image = UIImage(named: "MyNiceIcon")!.withColor(color: .black)
modifyAction.backgroundColor = .clear

MyNiceIcon stays white and the background of the button is gray. MyNiceIcon保持白色,按钮的背景为灰色。

Am I missing something? 我想念什么吗? Or can the color of the image used only be white? 还是可以仅使用白色的图像颜色? And the background color never transparent? 而且背景色永远不会透明?

I hope this will help you. 我希望这能帮到您。 You can set the background color of any swipe action plus you can even create a few of them on one swipe. 您可以设置任何滑动动作的背景颜色,甚至可以在一次滑动中创建一些背景颜色。 Simple example would be using something like this: 一个简单的例子就是使用这样的东西:

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

    return UISwipeActionsConfiguration(actions: [delete])
}

   @available(iOS 11.0, *)
    private func deleteAction(at indexPath: IndexPath) -> UIContextualAction {
        let action = UIContextualAction(style: .destructive, title: "Delete") { (action, view, completion) in
            let confirmDeletionAC = UIAlertController(title: "Delete", message: "Are you sure you want to delete this record?", preferredStyle: .alert)
            let confirmAlertAction = UIAlertAction(title: "Delete", style: .default, handler: { (action) in
                self.deleteObject(at: indexPath)
                self.historicTableView.deleteRows(at: [indexPath], with: .automatic)
                //                print("SWIPED TO DELETE")
                completion(true)
            })
            let cancelAlertAction = UIAlertAction(title: "Cancel", style: .cancel, handler: { (action) in
                //                print("DELETE CANCELLED")
                completion(false)
            })
            confirmDeletionAC.addAction(cancelAlertAction)
            confirmDeletionAC.addAction(confirmAlertAction)
            self.present(confirmDeletionAC, animated: true)
        }

        action.backgroundColor = UIColor.flatWatermelon

        return action
    }

This code comes from one of my projects and allows me to set a delete action from the right. 这段代码来自我的一个项目,使我可以从右侧设置删除动作。 If you want an action to come from the left simply use the same function but with leadingSwipeActionConfigurationForRowAt where you return the UISwipeActionsConfiguration, you use an array of UIContextualAction elements. 如果要使动作从左侧发出,只需使用相同的函数,但要使用带有leadingSwipeActionConfigurationForRowAt函数(在该函数中返回UISwipeActionsConfiguration),请使用UIContextualAction元素数组。

To set original image in action you can create custom class of UIImage and override method withRenderingMode as follow 要设置原始图像,您可以创建UIImage自定义类并使用withRenderingMode覆盖方法,如下所示

class OriginalImageRender: UIImage {
    override func withRenderingMode(_ renderingMode: UIImage.RenderingMode) -> UIImage {
        return self
    }
}

To use: 使用方法:

if let cgImageX =  UIImage(named: "MyNiceIcon")?.cgImage {
    modifyAction.image = OriginalImageRender(cgImage: cgImageX, scale: UIScreen.main.nativeScale, orientation: .up)
}

Above code will display image as original which added inside image assets. 上面的代码将图像显示为原始图像,并添加到图像资源中。

1- just add this class at the end of your class 1-只需在课程结束时添加此课程

class OriginalImageRender: UIImage {
override func withRenderingMode(_ renderingMode: UIImage.RenderingMode) -> UIImage {
    return self
}

} 2- create a cgImage from your UIImage } 2-从您的UIImage创建一个cgImage

let cgImageX =  UIImage(named: "delete")?.cgImage

3- pass this cgImage to class OriginalImageRender and add this image to your action 3-将此cgImage传递给类OriginalImageRender并将此图像添加到您的操作中

 action.image = OriginalImageRender(cgImage: cgImageX!)

It Worked for me, Hope helpful to you 对我有用,希望对您有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM