更改UITableView删除图像开关 [英] Change UITableView delete image swit

查看:86
本文介绍了更改UITableView删除图像开关的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要更改UITableView单元格的-按钮颜色并删除按钮(按下-按钮后显示)。我已经尝试过此答案 uitableview在iOS中删除按钮图片,但没有在UITableView编辑模式下工作。
请帮忙。

解决方案

我们可以进一步执行此操作:



1。)首先,添加UIView扩展

 扩展UIView {
func image() -> UIImage {
UIGraphicsBeginImageContextWithOptions(bounds.size,isOpaque,0)
保护let context = UIGraphicsGetCurrentContext()否则{
return UIImage()
}
layer.render( in:context)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
返回图片!
}

}



2.)第二个方法是在UITableView的下面添加以下委托方法。 -> UITableViewCellEditingStyle {
return .delete
}

覆盖func tableView(_ tableView:UITableView,editActionsForRowAt indexPath:IndexPath)-> [UITableViewRowAction]? {
let kCellActionWidth = CGFloat(70.0)//您想要删除按钮的宽度
let kCellHeight = tableView.frame.size.height //您想要删除按钮的高度
让空白= whitespaceString(width:kCellActionWidth)//添加填充


let deleteAction = UITableViewRowAction(style:.`default`,title:whitespace){_,_ in
/ /做任何你想做的动作
}

//从模式图像创建一种颜色,并将该颜色设置为动作的背景色
let view = UIView(frame:CGRect (x:tableView.frame.size.width-70,y:0,宽度:70,高度:kCellHeight))
view.backgroundColor = UIColor(红色:219.0 / 255.0,绿色:71.0 / 255.0,蓝色: 95.0 / 255.0,alpha:1.0)//视图
的背景色let imageView = UIImageView(frame:CGRect(x:15,
y:20,
width:40,
高度:40))
imageView.image = UIImage(named: xyz)! //所需的图片
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)->字符串{
let kPadding:CGFloat = 20
let mutable = NSMutableString(string:)
let attribute = [NSFontAttributeName:font]
而mutable.size(attributes:attribute ).width<宽度-(2 * kPadding){
mutable.append()
}
返回可变的字符串
}


I need to change UITableView Cell's "-" button color and delete button(which is showing after "-" button pressed) image. I have tried this answer uitableview delete button image in iOS but it doesn't work in UITableView edit mode. Please, help.

解决方案

We can do this in further steps:

1.) First, add a UIView extension

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.) second add these below delegates method of UITableView.

    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
}

这篇关于更改UITableView删除图像开关的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆