更改UITableView删除图像swit

时间:2017-04-26 08:35:38

标签: ios swift uitableview delete-row

我需要更改UITableView Cell"" - "按钮颜色和删除按钮(在&#34后显示; - "按下按钮)图像。我尝试过这个答案uitableview delete button image in iOS,但它在UITableView编辑模式下无效。 请帮忙。

2 个答案:

答案 0 :(得分:5)

我们可以在以下步骤中执行此操作:

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。)第二步添加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
}

答案 1 :(得分:0)

在自定义单元格中覆盖此功能

org.springframework.data.mongodb.core.convert.mongoConverter

希望这有帮助。 enter image description here