UISearchBar 不在 TableView 中查找信息 [英] UISearchBar not looking for information in TableView

查看:16
本文介绍了UISearchBar 不在 TableView 中查找信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试从 firebase 获取数据并显示在 tableView 中.一切都很好.但是我需要使用 UISearchBar 在 tableView 中进行搜索.我应该解决两个问题.

I try to fetch data from firebase and show in tableView. Everything is good. But I need to search in tableView using UISearchBar. I should to solve two problems.

1) 当我开始输入时,我的 tableView 消失了.里面的所有信息都没有显示

1) When I start typing my tableView disappears. all the information in it is not displayed

2) 当我删除 UISearchBar 中的所有内容时,什么都没有出现.

2) When I erase everything in UISearchBar, nothing appears.

import UIKit
import SPStorkController
import Firebase

class FoodViewController: UIViewController {

    override var preferredStatusBarStyle: UIStatusBarStyle { return .lightContent }
    let tableView = UITableView()
    var foods = [FoodModel]()
    var searchedFoods = [String]()
    var searching = false

    override func viewDidLoad() {
        super.viewDidLoad()

        fetchFoods()

        self.modalPresentationCapturesStatusBarAppearance = true
        self.view.backgroundColor = UIColor.white
        let controller = UIViewController()

        self.tableView.delegate = self
        self.tableView.dataSource = self
        self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "Cell")            

        self.view.addSubview(self.tableView)

        tableView.delegate = self
        tableView.dataSource = self

        let searchBar = UISearchBar(frame: CGRect(x: 0, y: 5, width: 350, height: 40))
        searchBar.searchBarStyle = .minimal
        searchBar.delegate = self

        self.view.addSubview(searchBar)            
    }

    override func viewWillLayoutSubviews() {
        super.viewWillLayoutSubviews()
        self.updateLayout(with: self.view.frame.size)
    }

    func updateLayout(with size: CGSize) {
        self.tableView.frame = CGRect.init(x: 0, y: 45, width: size.width, height: 400)            
    }

    func fetchFoods() {
        Database.database().reference().child("food").observe(.childAdded) { (snapshot) in
            if let dict = snapshot.value as? [String: AnyObject] {
                let newTitle = dict["title"] as! String
                let exCell = FoodModel(title: newTitle)
                self.foods.append(exCell)
                DispatchQueue.main.async {
                    self.tableView.reloadData()                        
                }
            }                
        }            
    }
}

extension FoodViewController: UITableViewDataSource { 

   func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)

        if searching {
            cell.textLabel?.text = searchedFoods[indexPath.row]
        } else {
            let food = foods[indexPath.item]
            cell.textLabel?.text = food.title
        }
        return cell
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if searching {
            return searchedFoods.count
        } else {
            return foods.count
        }
    }

    func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
    }
}

extension FoodViewController: UITableViewDelegate {

    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        if scrollView == self.tableView {
            SPStorkController.scrollViewDidScroll(scrollView)
        }
    }
}

extension FoodViewController: UISearchBarDelegate {

    func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
        //searchedFoods = foods.filter({$0.lowercased().prefix(searchText.count) == searchText.lowercased()})
        searching = true
        tableView.reloadData()
    }

    func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
        searching = false
        searchBar.text = ""
        tableView.reloadData()
    }        
}`

这是我的模型

class FoodModel {

    var title = String()

    init (title: String) {

        self.title = title
    }
}

推荐答案

searchedFoods 应该是一个对象数组,与主食物列表相同.

searchedFoods should be an array of objects, the same as the main foods list.

我认为你只需要更新你的过滤

I think you just need to update your filtering

所以将 searchedFoods 改为 [FoodModel]()

然后将过滤器更改为:

searchedFoods = foods.filter({ $0.title.lowercased().prefix(searchText.count) == searchText.lowercased() })

这篇关于UISearchBar 不在 TableView 中查找信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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