如何将自定义图像添加到uitableview单元格滑动以进行删除

时间:2015-03-29 21:32:16

标签: uitableview swift custom-cell

你能否告诉我,在UITableview上滑动单元格时如何添加自定义图像以删除按钮?

3 个答案:

答案 0 :(得分:6)

搜索您需要的功能" editActionsForRowAtIndexPath",您可以在其中创建操作范围。您需要将UIImage设置为UITableViewRowAction的backgroundColor。

let someAction = UITableViewRowAction(style: .Default, title: "") { value in 
    println("button did tapped!")
}
someAction.backgroundColor = UIColor(patternImage: UIImage(named: "myImage")!)

答案 1 :(得分:0)

您可以使用以下UITableView委托函数:

@available(iOS 11.0, *)
func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
    let deleteAction = UIContextualAction(style: .normal, title: "", handler: {a,b,c in
        // example of your delete function
        self.YourArray.remove(at: indexPath.row)
        tableView.deleteRows(at: [indexPath], with: .automatic)
    })

    deleteAction.image = UIImage(named: "trash.png")
    deleteAction.backgroundColor = .red
    return UISwipeActionsConfiguration(actions: [deleteAction])
}

PS:就我个人而言,我认为32号图标是最好的

答案 2 :(得分:0)

具有自定义图像和背景尺寸的图像大小的100%工作可滑动单元格ios swift #ios #swift#ios13#ios14

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

        let action =  UIContextualAction(style: .normal, title: "", handler: { (action,view,completionHandler ) in
            self.selectedIndex  = indexPath.row
            self.deleteNotification()
            completionHandler(true)

        })
        
        if #available(iOS 13.0, *) {
            action.image = UIGraphicsImageRenderer(size: CGSize(width: 30, height: 30)).image { _ in
                UIImage(named: "delete-1")?.draw(in: CGRect(x: 0, y: 0, width: 30, height: 30))
            }
            action.backgroundColor = UIColor.init(red: 0/255.0, green: 0/255.0, blue: 0/255.0, alpha: 0.0)
            let confrigation = UISwipeActionsConfiguration(actions: [action])
            return confrigation
        } else {
            // Fallback on earlier versions
            let cgImageX =  UIImage(named: "delete-1")?.cgImage
            action.image = OriginalImageRender(cgImage: cgImageX!)
            action.backgroundColor = UIColor.init(hex: "F7F7F7")
            let confrigation = UISwipeActionsConfiguration(actions: [action])

            return confrigation
        }
}