Передача данных в панель поиска - PullRequest
0 голосов
/ 24 апреля 2019

У меня есть 2 viewController, которые нравятся друг другу.Первый viewController имеет кнопку collectionView as, поэтому при нажатии на collectionView он направит вас ко второму viewContoller через segue.

  @IBAction func presedCollectionViewButton(_ sender: Any) {
    performSegue(withIdentifier: "segue", sender: self)
  }

   override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

    let destinationVC = segue.destination as! AllCategoriesViewController
    destinationVC.filterText = test

}

Затем я передал данные в searchBar, который во втором controllView для фильтрацииtableView после выполнения segue выполняется, но он не фильтрует tableView напрямую, и для того, чтобы увидеть фильтр, который я передал, я должен нажать на searchBar в viewController.

extension AllCategoriesViewController: UISearchBarDelegate {

     func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
        searchBar.text = filterText
        filteredCategory = allcotegoryLabel.filter({$0.categoryName.prefix(searchText.count) == searchText})
        isSearch = true
        allCategoriesTableView.reloadData()
    }

    func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
        isSearch = false
        searchBar.text = ""
        allCategoriesTableView.reloadData()
    }
}

Так что мой вопроскак решить эту проблему и позволить ему фильтровать tableView после выполнения перехода к преформе?

  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "allCategories", for: indexPath) as! allCategoriesTableViewCell
    if isSearch {
       cell.allCategoriesLabel.text = filteredCategory[indexPath.row].categoryName
    } else {
       cell.allCategoriesLabel.text = allcotegoryLabel[indexPath.row].categoryName
    }
    return cell
}

здесь используется isSearch

1 Ответ

0 голосов
/ 24 апреля 2019

Вы должны установить переменную в строке поиска, а не в строку поиска.Затем установите текст панели поиска в методе viewDidLoad(), после чего обновите tableView в viewDidLoad.Если это не сработает, вы можете попробовать то же самое в viewDidAppear.

...