【问题标题】:Change UITableView delete image swit更改 UITableView 删除图片切换
【发布时间】:2017-09-23 13:06:42
【问题描述】:

我需要更改 UITableView Cell 的“-”按钮颜色并删除按钮(在“-”按钮按下后显示)图像。我试过这个答案uitableview delete button image in iOS,但它在 UITableView 编辑模式下不起作用。 请帮忙。

【问题讨论】:

    标签: ios swift uitableview delete-row


    【解决方案1】:

    我们可以在进一步的步骤中做到这一点:

    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
    }
    

    【讨论】:

    • @AlexCrow 很高兴来帮忙?
    【解决方案2】:

    在您的自定义单元格中覆盖此函数

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

    希望对您有所帮助。

    【讨论】:

    • 它根本没有帮助。在 swift 3 中没有这样的值 - UITableViewCellDeleteConfirmationControl。并且状态不等于.showingDeleteConfirmationMask
    猜你喜欢
    • 1970-01-01
    • 2022-10-07
    • 2011-06-11
    • 2013-02-06
    • 2011-11-15
    • 2020-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多