Swift - 在 Button-Tap 后从 TableView 中删除单元格 [英] Swift - remove cell from TableView after Button-Tap

查看:20
本文介绍了Swift - 在 Button-Tap 后从 TableView 中删除单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的项目中,我有一个 UITableView.其中的 cells 包含一个 UIButton,它的作用类似于复选框",因此用户可以勾选任务.

in my project I have a UITableView. The cells inside of that contains among other things a UIButton which acts like a "check-box", so the user can tick of a task.

我的问题:如何在用户按下 UIButton 里面的 cell 后删除它?

My question: How can I delete a cell after the user presses the UIButton inside of it?

这是我的customCell:

import UIKit

class WhishCell: UITableViewCell {

    let label: UILabel = {
       let v = UILabel()
        v.font = UIFont(name: "AvenirNext", size: 23)
        v.textColor = .white
        v.font = v.font.withSize(23)
        v.translatesAutoresizingMaskIntoConstraints = false
        return v
    }()

        let checkButton: UIButton =  {
        let v = UIButton()
        v.backgroundColor = .darkGray
//        v.layer.borderColor = UIColor.red.cgColor
//        v.layer.borderWidth = 2.0
        v.translatesAutoresizingMaskIntoConstraints = false
        v.setBackgroundImage(UIImage(named: "boxUnchecked"), for: .normal)

        return v
    }()



        public static let reuseID = "WhishCell"

        required init?(coder: NSCoder) {fatalError("init(coder:) has not been implemented")}

        override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
            super.init(style: style, reuseIdentifier: reuseIdentifier)

            self.backgroundColor = .clear

            // add checkButton
            self.contentView.addSubview(checkButton)
            self.checkButton.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: 20).isActive = true
            self.checkButton.centerYAnchor.constraint(equalTo: self.centerYAnchor).isActive = true
            self.checkButton.widthAnchor.constraint(equalToConstant: 40).isActive = true
            self.checkButton.heightAnchor.constraint(equalToConstant: 40).isActive = true
            self.checkButton.addTarget(self, action: #selector(checkButtonTapped), for: .touchUpInside)
            // add label
            self.contentView.addSubview(label)
            self.label.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: 70).isActive = true
            self.label.centerYAnchor.constraint(equalTo: self.centerYAnchor).isActive = true



        }

        @objc func checkButtonTapped(){
            self.checkButton.setBackgroundImage(UIImage(named: "boxChecked"), for: .normal)
            self.checkButton.alpha = 0
            self.checkButton.transform =  CGAffineTransform(scaleX: 1.3, y: 1.3)

            UIView.animate(withDuration: 0.3) {
                self.checkButton.alpha = 1
                self.checkButton.transform = CGAffineTransform.identity
            }


        }
    }

推荐答案

您可以使用此扩展名访问单元格内的 tableViewindexPath:

You can access the tableView and the indexPath within the cell with this extension:

extension UITableViewCell {
    var tableView: UITableView? {
        return (next as? UITableView) ?? (parentViewController as? UITableViewController)?.tableView
    }

    var indexPath: IndexPath? {
        return tableView?.indexPath(for: self)
    }
}

在这个其他扩展的帮助下:

With the help of this other extension:

extension UIView {
    var parentViewController: UIViewController? {
        var parentResponder: UIResponder? = self
        while parentResponder != nil {
            parentResponder = parentResponder!.next
            if let viewController = parentResponder as? UIViewController {
                return viewController
            }
        }
        return nil
    }
}

这样你就可以照常删除单元格了:

So then you can delete the cell as usual:

tableView!.deleteRows(at: [indexPath!], with: .automatic)  

注意tableView应该负责管理单元格.

Note that the tableView should be responsible for managing cells.

这篇关于Swift - 在 Button-Tap 后从 TableView 中删除单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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