3

我正在使用 Algolia InstantSearch 并且 HitsTableViewController 正在正确显示数据列表,但是结果不会随着每次击键而改变。有谁知道如何使输入到搜索栏中的每个字母都改变结果?我已经完成了 InstantSearch 文档所写的所有内容。

这是代码

import UIKit
import InstantSearch

struct BestBuyItem: Codable {
  let name: String
}

struct BestBuyTableViewCellConfigurator: TableViewCellConfigurable {
   
  let model: BestBuyItem
  
  init(model: BestBuyItem, indexPath: IndexPath) {
    self.model = model
  }
  
  func configure(_ cell: UITableViewCell) {
    cell.textLabel?.text = model.name
  }

}

typealias BestBuyHitsViewController = HitsTableViewController<BestBuyTableViewCellConfigurator>

class ViewController: UIViewController {
      
  let searcher = SingleIndexSearcher(appID: "latency",
                                     apiKey: "1f6fd3a6fb973cb08419fe7d288fa4db",
                                     indexName: "bestbuy")
  lazy var searchController: UISearchController = .init(searchResultsController: hitsViewController)
  lazy var searchConnector: SingleIndexSearchConnector<BestBuyItem> = .init(searcher: searcher,
                                                                            searchController: searchController,
                                                                            hitsController: hitsViewController,
                                                                            filterState: filterState)
  let hitsViewController: BestBuyHitsViewController = .init()
  let statsInteractor: StatsInteractor = .init()
  let filterState: FilterState = .init()
  lazy var categoryConnector: FacetListConnector = .init(searcher: searcher,
                                                         filterState: filterState,
                                                         attribute: "category",
                                                         operator: .or,
                                                         controller: categoryListController,
                                                         presenter: FacetListPresenter(sortBy: [.isRefined]))
  
  lazy var categoryListController: FacetListTableController = .init(tableView: categoryTableViewController.tableView)
  let categoryTableViewController: UITableViewController = .init()
  
  override func viewDidLoad() {
    super.viewDidLoad()
    searchConnector.connect()
    categoryConnector.connect()
    statsInteractor.connectSearcher(searcher)
    statsInteractor.connectController(self)
    searcher.search()
    
    setupUI()
  }
  
  override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    searchController.searchBar.becomeFirstResponder()
  }
  
  func setupUI() {
    view.backgroundColor = .white
    navigationItem.searchController = searchController
    navigationItem.rightBarButtonItem = .init(title: "Category", style: .plain, target: self, action: #selector(showFilters))
    searchController.hidesNavigationBarDuringPresentation = false
    searchController.showsSearchResultsController = true
    searchController.automaticallyShowsCancelButton = false
    categoryTableViewController.title = "Category"
  }
  
  @objc func showFilters() {
    let navigationController = UINavigationController(rootViewController: categoryTableViewController)
    categoryTableViewController.navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(dismissFilters))
    present(navigationController, animated: true, completion: .none)
  }
  
  @objc func dismissFilters() {
    categoryTableViewController.navigationController?.dismiss(animated: true, completion: .none)
  }
  
}

extension ViewController: StatsTextController {
  
  func setItem(_ item: String?) {
    title = item
  }
  
}

我真的很感谢任何有帮助的人,谢谢

4

0 回答 0