我有 2 个互相喜欢的 viewController。第一个viewController有一个collectionView作为按钮,所以当你点击collectionView时,它会通过segue引导你到第二个viewContoller。
@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
}
然后我将数据传递到第二个controllView中的searchBar,以便在执行执行segue后过滤tableView,但它不会直接过滤tableView,为了查看我传递的filterText,我必须单击在 viewController 的 searchBar 上。
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()
}
}
所以我的问题是如何解决这个问题并让它在执行预成型segue后过滤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 的一些用法
最佳答案
您应该将搜索栏文本设置为变量而不是搜索栏。然后,在viewDidLoad()方法中设置搜索栏文本,然后在viewDidLoad方法中刷新tableView。如果这不起作用,您可以在 viewDidAppear
中尝试相同的操作。
关于ios - 将数据传递到搜索栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55819617/