在滑动时禁用跳出效果以删除UITableView中的UITableViewCell

时间:2018-03-07 23:59:56

标签: ios swift uitableview uicollectionviewcell

我正在UITableViewCell中的所有UITableView上实施滑动以删除功能。同时我想使用自定义按钮,为此,本教程派上用场:Custom edit view in UITableViewCell while swipe left. Objective-C or Swift (我选择了第4个解决方案)。到目前为止,我对我的总体结果感到满意,因为它运作得很好。但是,我注意到在我的情况下,当在单元格上滑动时,单元格移动并不会在隐藏按钮显示之后停止,而是继续并且它会显示一些灰色空间。一旦细胞释放,它会反弹回来,覆盖灰色空间。最大的问题是,如何禁用此弹跳效果并在按钮显示后让单元格停在幻灯片上? 我的问题非常类似于How to remove UITableViewCell swipe to delete bounce 。但是,我已经尝试了所有UISwipeViewDelegate解决方案,但这些解决方案都不适用于我。

我需要它是什么样的: enter image description here

它实际上是什么样的: enter image description here

这是我的代码:

import UIKit


class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource{

    @IBOutlet weak var tableView: UITableView!
    lazy var rowHeight: CGFloat = {
       return 82
    }()

    override func viewDidLoad() {
        super.viewDidLoad()

        tableView.dataSource = self
        tableView.delegate = self

        let nibName = UINib(nibName: "TableViewCell", bundle: nil)
        tableView.register(nibName, forCellReuseIdentifier: "cell")
    }


    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }


    func numberOfSections(in tableView: UITableView) -> Int {
        return 20
    }


    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TableViewCell
        cell.makeCellWith(image: "sampleImage", userName: "userNameSample")

        return cell
    }


    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return rowHeight
    }



    // MARK: - Cell Sections
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        view.frame = UIEdgeInsetsInsetRect(view.frame, UIEdgeInsetsMake(0, 10, 0, 10))
    }


    // Set the spacing between sections
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 10
    }


    // Make the background color show through
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let headerView = UIView()
        headerView.backgroundColor = UIColor.clear
        return headerView
    }



    // MARK: - Slide to delete feature
    fileprivate func whitespaceString(font: UIFont = UIFont.systemFont(ofSize: 15), width: CGFloat) -> String {
        let padding: CGFloat = 20
        let mutable = NSMutableString(string: "")
        let attribute = [NSAttributedStringKey.font: font]
        while mutable.size(withAttributes: attribute).width < width - (2 * padding) {
            mutable.append(" ")
        }
        return mutable as String
    }


    func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
        let whitespace = whitespaceString(width: rowHeight)
        let deleteAction = UITableViewRowAction(style: .default, title: whitespace) { (action, indexPath) in
            // do action on delete here
        }

        // create a color from pattern image and set the color as a background color of action
        let view = UIView(frame: CGRect(x: 0, y: 0, width: rowHeight, height: rowHeight))
        view.backgroundColor = UIColor.white

        let imageSize: CGFloat = 50

        let imageView: UIImageView = {
            let iv = UIImageView()
            let buttonImage = UIImage(named: "plusButton")
            iv.image = buttonImage
            return iv
        }()

        imageView.frame = CGRect(x: (view.frame.height - imageSize)/2,
                          y: (view.frame.width - imageSize)/2,
                          width: imageSize,
                          height: imageSize)
        view.addSubview(imageView)

        let image = view.image()
        deleteAction.backgroundColor = UIColor(patternImage: image)
        return [deleteAction]
    }


    // MARK: - Delete Object
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}



fileprivate 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!
    }
}

0 个答案:

没有答案