点击搜索结果时搜索结果消失

时间:2020-09-08 03:39:23

标签: ios swift xcode uitableview uisearchbar

我正在使用UITableView并在其顶部带有搜索栏(搜索栏是通过编程方式设置的)。我可以查看搜索结果,但是当我在搜索栏外点击时,结果将被清除。我敢肯定,实现起来很简单,但是我无法弄清楚。任何帮助或反馈表示赞赏!

//
//  InventoryViewController.swift
//  ItemizePro
//
//  Created by Tyler Wasick on 5/18/20.
//  Copyright © 2020 Tyler Wasick. All rights reserved.
//

import UIKit

class InventoryViewController: UIViewController {
    
    @IBOutlet weak var inventoryTableView: UITableView!
    
    var assetList = [Asset]()
    let searchController = UISearchController(searchResultsController: nil)
    var filterAssetList = [Asset]()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        // Configure the tableview delegate and data source
        inventoryTableView.delegate = self
        inventoryTableView.dataSource = self
        
        // Download asset list
        FirebaseDB.downloadAssetList { (list) in
            self.assetList = list
            self.inventoryTableView.reloadData()
        }
        
        // Setup Search Delegate
        searchController.searchResultsUpdater = self
        definesPresentationContext = true
        inventoryTableView.tableHeaderView = searchController.searchBar
        
    }
    
    // MARK: - Functions
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        print(indexPath)
    }
    
    
    
    // Search bar function, looks to match name from the search to asset name
    private func filterAssets(for searchText:String) {
        filterAssetList = assetList.filter({ (asset) -> Bool in
            return (asset.name?.lowercased().contains(searchText.lowercased()))!
        })
        inventoryTableView.reloadData()
    }
    
    

}

extension InventoryViewController: UITableViewDelegate, UITableViewDataSource, UISearchResultsUpdating {
    
    func updateSearchResults(for searchController: UISearchController) {
        filterAssets(for: searchController.searchBar.text ?? "")
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        // If there is no search use the "assetList" array, if not use "filterAssetList" array
        if filterAssetList.count == 0 {
            return assetList.count
        } else {
            return filterAssetList.count
        }
         
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        // Format date field
        let formatter = DateFormatter()
        formatter.dateStyle = .medium
        formatter.timeStyle = .none
                
        // Set the cell with asset data
        let cell = tableView.dequeueReusableCell(withIdentifier: Constants.Storyboards.TableViewCell.inventoryCellID) as! InventoryListView
        
        // Get the asset for that rown
        // Initiate emtpy variable
        var cellAsset = Asset()
        if filterAssetList.count == 0 {
            cellAsset = assetList[indexPath.row]
        } else {
            cellAsset = filterAssetList[indexPath.row]
        }
        
        // Set the cell details
        cell.setInventoryCell(cellAsset)

        // Return the row
        return cell
    }
}

1 个答案:

答案 0 :(得分:1)

我发现创建变量标志@Query("MATCH (a:Foo {id: \$id})-[r]->(o) RETURN a, collect(r), collect(o)") fun findByIdOrNull(id: String): FooNode? 并使用它而不是filterAssetList计数更容易。此标志跟踪“搜索栏” 我将此教程作为参考,对我有很大帮助:)
https://daddycoding.com/2018/12/07/ios-tutorials-search-your-table-using-a-searchbar/

https://github.com/zhiyao92/Search-Bar

var isSearching = false