0

当我使用tableView:editActionsForRowAt:在 iOS 应用程序中提供一些有用的功能时。

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

我已经到了与上面类似的其他一些 API 的点,但是在向右滑动时触发,而不是像tableView:editActionsForRowAt:这样的向左滑动会很有用。像这样说:

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

浏览网络我发现了 UITableViewDelegate 协议的这两种方法:

tableView:leadingSwipeActionsConfigurationForRowAt
tableView:trailingSwipeActionsConfigurationForRowAt

经过一些基本的实验,我想我可以得到我想要的,使用tableView:leadingSwipeActionsConfigurationForRowAt,因此在单元格的右侧滑动时为用户提供更多功能。但似乎这种方法并没有提供太多的自由。例如,这里是我的代码(非常受我在互联网上找到的一些示例的启发):

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

即使存在这些行,这里:

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

MyNiceIcon 保持白色,按钮的背景为灰色。

我错过了什么吗?还是只能使用白色的图像颜色?而且背景颜色永远不透明?

4

3 回答 3

1

要设置原始图像,您可以创建自定义类UIImage和覆盖方法withRenderingMode,如下所示

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

要使用:

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

上面的代码将图像显示为添加到图像资产中的原始图像。

于 2019-09-10T08:08:10.137 回答
0

1-只需在课程结束时添加这个课程

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

2-从你的 UIImage 创建一个 cgImage

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

3-将此 cgImage 传递给 OriginalImageRender 类并将此图像添加到您的操作中

 action.image = OriginalImageRender(cgImage: cgImageX!)

它对我有用,希望对你有帮助

于 2019-09-11T10:26:42.563 回答
0

我希望这能帮到您。您可以设置任何滑动动作的背景颜色,甚至可以在一次滑动中创建其中的一些。简单的例子是使用这样的东西:

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

此代码来自我的一个项目,允许我从右侧设置删除操作。如果您希望一个动作来自左侧,只需使用相同的函数,但leadingSwipeActionConfigurationForRowAt在返回 UISwipeActionsConfiguration 的位置,您可以使用 UIContextualAction 元素数组。

于 2019-09-10T07:53:42.157 回答