Ios 更改UITableView删除图像开关

Ios 更改UITableView删除图像开关,ios,swift,uitableview,delete-row,Ios,Swift,Uitableview,Delete Row,我需要更改UITableView单元格的“-”按钮颜色并删除按钮(在按下“-”按钮后显示)图像。我尝试过这个答案,但在UITableView编辑模式下不起作用。 请提供帮助。在自定义单元格中重写此函数 override func didTransition(to state: UITableViewCellStateMask) { super.willTransition(to: state) if state == .showingDeleteConfirmationMask

我需要更改UITableView单元格的“-”按钮颜色并删除按钮(在按下“-”按钮后显示)图像。我尝试过这个答案,但在UITableView编辑模式下不起作用。
请提供帮助。

在自定义单元格中重写此函数

override func didTransition(to state: UITableViewCellStateMask) {
    super.willTransition(to: state)

    if state == .showingDeleteConfirmationMask {
        for subview: UIView in subviews {
            if NSStringFromClass(type(of: subview)) == "UITableViewCellDeleteConfirmationView" {
                let deleteBtn = UIImageView(frame: CGRect(x: 0, y: 0, width: 84, height: 43))
                deleteBtn.backgroundColor = .red
                deleteBtn.contentMode = .scaleAspectFit

                deleteBtn.image = UIImage(named: "delete.png")
                subview.subviews[0].addSubview(deleteBtn)
            }
        }
    }
}
希望这有帮助。
在自定义单元格中重写此函数

override func didTransition(to state: UITableViewCellStateMask) {
    super.willTransition(to: state)

    if state == .showingDeleteConfirmationMask {
        for subview: UIView in subviews {
            if NSStringFromClass(type(of: subview)) == "UITableViewCellDeleteConfirmationView" {
                let deleteBtn = UIImageView(frame: CGRect(x: 0, y: 0, width: 84, height: 43))
                deleteBtn.backgroundColor = .red
                deleteBtn.contentMode = .scaleAspectFit

                deleteBtn.image = UIImage(named: "delete.png")
                subview.subviews[0].addSubview(deleteBtn)
            }
        }
    }
}
希望这有帮助。

我们可以通过进一步的步骤来实现这一点:

1.)首先,添加UIView扩展

extension UIView {
    func image() -> UIImage {
       UIGraphicsBeginImageContextWithOptions(bounds.size, isOpaque, 0)
       guard let context = UIGraphicsGetCurrentContext() else {
        return UIImage()
       }
       layer.render(in: context)
       let image = UIGraphicsGetImageFromCurrentImageContext()
       UIGraphicsEndImageContext()
       return image!
   }
}

2.)其次,将这些添加到UtableView方法的下面

    override func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle {
        return .delete
}

override func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
    let kCellActionWidth = CGFloat(70.0)// The width you want of delete button
    let kCellHeight = tableView.frame.size.height // The height you want of delete button
    let whitespace = whitespaceString(width: kCellActionWidth) // add the padding 


    let deleteAction = UITableViewRowAction(style: .`default`, title: whitespace) {_,_ in
        // do whatever the action you want
    }

    // create a color from patter image and set the color as a background color of action
    let view = UIView(frame: CGRect(x: tableView.frame.size.width-70, y: 0, width: 70, height: kCellHeight))
    view.backgroundColor = UIColor(red: 219.0/255.0, green: 71.0/255.0, blue: 95.0/255.0, alpha: 1.0) // background color of view
    let imageView = UIImageView(frame: CGRect(x: 15,
                                              y: 20,
                                              width: 40,
                                              height: 40))
    imageView.image = UIImage(named: "xyz")! // required image
    view.addSubview(imageView)
    let image = view.image()

    deleteAction.backgroundColor = UIColor.init(patternImage: image)
    return [deleteAction]

}

fileprivate func whitespaceString(font: UIFont = UIFont.systemFont(ofSize: 15), width: CGFloat) -> String {
    let kPadding: CGFloat = 20
    let mutable = NSMutableString(string: "")
    let attribute = [NSFontAttributeName: font]
    while mutable.size(attributes: attribute).width < width - (2 * kPadding) {
        mutable.append(" ")
    }
    return mutable as String
}
override func tableView(u tableView:UITableView,editingStyleForRowAt indexath:indexPath)->UITableViewCellEditingStyle{
返回。删除
}
重写func tableView(tableView:UITableView,EditActionsErrorwat indexPath:indexPath)->[UITableViewRowAction]?{
让kCellationWidth=CGFloat(70.0)//删除按钮所需的宽度
让kCellHeight=tableView.frame.size.height//删除按钮所需的高度
让whitespace=whitespaceString(宽度:kCellationWidth)//添加填充
让deleteAction=UITableViewRowAction(样式:.`default`,标题:空格){
//做你想做的任何事
}
//从图案图像创建颜色,并将该颜色设置为动作的背景色
let view=UIView(frame:CGRect(x:tableView.frame.size.width-70,y:0,width:70,height:kCellHeight))
view.backgroundColor=UIColor(红色:219.0/255.0,绿色:71.0/255.0,蓝色:95.0/255.0,alpha:1.0)//视图的背景色
让imageView=UIImageView(帧:CGRect(x:15,
y:20,
宽度:40,
身高:40)
imageView.image=UIImage(名为:“xyz”)!//所需图像
view.addSubview(imageView)
让image=view.image()
deleteAction.backgroundColor=UIColor.init(patternImage:图像)
返回[删除操作]
}
fileprivate func whitespaceString(字体:UIFont=UIFont.systemFont(大小:15),宽度:CGFloat)->String{
让kPadding:CGFloat=20
让mutable=NSMutableString(字符串:“”)
let属性=[NSFontAttributeName:font]
而可变.size(属性:attribute).width
我们可以在后续步骤中执行此操作:

1.)首先,添加UIView扩展

extension UIView {
    func image() -> UIImage {
       UIGraphicsBeginImageContextWithOptions(bounds.size, isOpaque, 0)
       guard let context = UIGraphicsGetCurrentContext() else {
        return UIImage()
       }
       layer.render(in: context)
       let image = UIGraphicsGetImageFromCurrentImageContext()
       UIGraphicsEndImageContext()
       return image!
   }
}

2.)其次,将这些添加到UtableView方法的下面

    override func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle {
        return .delete
}

override func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
    let kCellActionWidth = CGFloat(70.0)// The width you want of delete button
    let kCellHeight = tableView.frame.size.height // The height you want of delete button
    let whitespace = whitespaceString(width: kCellActionWidth) // add the padding 


    let deleteAction = UITableViewRowAction(style: .`default`, title: whitespace) {_,_ in
        // do whatever the action you want
    }

    // create a color from patter image and set the color as a background color of action
    let view = UIView(frame: CGRect(x: tableView.frame.size.width-70, y: 0, width: 70, height: kCellHeight))
    view.backgroundColor = UIColor(red: 219.0/255.0, green: 71.0/255.0, blue: 95.0/255.0, alpha: 1.0) // background color of view
    let imageView = UIImageView(frame: CGRect(x: 15,
                                              y: 20,
                                              width: 40,
                                              height: 40))
    imageView.image = UIImage(named: "xyz")! // required image
    view.addSubview(imageView)
    let image = view.image()

    deleteAction.backgroundColor = UIColor.init(patternImage: image)
    return [deleteAction]

}

fileprivate func whitespaceString(font: UIFont = UIFont.systemFont(ofSize: 15), width: CGFloat) -> String {
    let kPadding: CGFloat = 20
    let mutable = NSMutableString(string: "")
    let attribute = [NSFontAttributeName: font]
    while mutable.size(attributes: attribute).width < width - (2 * kPadding) {
        mutable.append(" ")
    }
    return mutable as String
}
override func tableView(u tableView:UITableView,editingStyleForRowAt indexath:indexPath)->UITableViewCellEditingStyle{
返回。删除
}
重写func tableView(tableView:UITableView,EditActionsErrorwat indexPath:indexPath)->[UITableViewRowAction]?{
让kCellationWidth=CGFloat(70.0)//删除按钮所需的宽度
让kCellHeight=tableView.frame.size.height//删除按钮所需的高度
让whitespace=whitespaceString(宽度:kCellationWidth)//添加填充
让deleteAction=UITableViewRowAction(样式:.`default`,标题:空格){
//做你想做的任何事
}
//从图案图像创建颜色,并将该颜色设置为动作的背景色
let view=UIView(frame:CGRect(x:tableView.frame.size.width-70,y:0,width:70,height:kCellHeight))
view.backgroundColor=UIColor(红色:219.0/255.0,绿色:71.0/255.0,蓝色:95.0/255.0,alpha:1.0)//视图的背景色
让imageView=UIImageView(帧:CGRect(x:15,
y:20,
宽度:40,
身高:40)
imageView.image=UIImage(名为:“xyz”)!//所需图像
view.addSubview(imageView)
让image=view.image()
deleteAction.backgroundColor=UIColor.init(patternImage:image)
返回[删除操作]
}
fileprivate func whitespaceString(字体:UIFont=UIFont.systemFont(大小:15),宽度:CGFloat)->String{
让kPadding:CGFloat=20
让mutable=NSMutableString(字符串:“”)
let属性=[NSFontAttributeName:font]
而可变.size(属性:attribute).width
这根本没用。在swift 3中没有此类值-
UITableViewCellDeleteConfirmationControl
。并且state不等于
。showingDeleteConfirmationMask
根本没有帮助。在swift 3中没有此类值-
UITableViewCellDeleteConfirmationControl
。状态不等于
。showingDeleteConfirmationMask
@AlexCrow乐意帮助@AlexCrow乐意帮助