从 TableView 中过滤数据

Filter data from TableView

如何通过搜索栏过滤 tableView?我从 Firebase 数据库获取数据到 tableView,我的搜索栏在 navigationBar.

这是我的代码:

class TableViewCustomer: UIViewController, UITableViewDelegate, UITableViewDataSource {

    var ref:DatabaseReference!
    var customerList = [CustomerModel]()
    lazy var searchBar: UISearchBar = UISearchBar(frame: CGRect(x: 0, y: 0, width: 300, height: 20))


    @IBOutlet weak var tableViewCustomer: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()

        searchBar.placeholder = "Search"
        navigationItem.titleView = searchBar

        if let textfield = searchBar.value(forKey: "searchField") as? UITextField {
            textfield.textColor = UIColor.gray
            if let backgroundview = textfield.subviews.first {
                backgroundview.backgroundColor = UIColor.white
                backgroundview.layer.cornerRadius = 18
                backgroundview.clipsToBounds = true
            }
        }

        tableViewCustomer.delegate = self
        tableViewCustomer.dataSource = self

        ref = Database.database().reference().child("Customer");

        ref.observe(DataEventType.value, with: { (snapshot) in
            if snapshot.childrenCount > 0 {
                self.customerList.removeAll()
                for results in snapshot.children.allObjects as! [DataSnapshot] {
                    let results = results.value as? [String: AnyObject]
                    let name = results?["Name and surname"]
                    let phone = results?["Phone"]
                    let company = results?["Company name"]
                    let myCustomer = CustomerModel(name: name as? String, phone: phone as? String, company: company as? String)
                    self.customerList.append(myCustomer)
                }
                self.tableViewCustomer.reloadData()
            }
        })
    }

    override var preferredStatusBarStyle: UIStatusBarStyle {
        return .lightContent
    }

    @objc func buttonAction(sender: UIButton!) {
        dismiss(animated: true, completion: nil)
    }

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

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

    public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableViewCustomer.dequeueReusableCell(withIdentifier: "cellCustomer") as! TableViewCellCustomer
        cell.layer.backgroundColor = UIColor.init(white: 1, alpha: 0.7).cgColor
        let customer: CustomerModel
        customer = customerList[indexPath.row]
        cell.nameLabel.text = customer.name
        cell.phoneLabel.text = customer.phone
        cell.companyLabel.text = customer.company

        return cell
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        performSegue(withIdentifier: "description", sender: self)
    }
}

添加您的搜索栏和一个空数组。当您搜索时,将合适的项目添加到空数组中,然后将它们从数组中删除。使用新数组索引到您的 UI。