在 Swift 3 中过滤 UICollectionview 上的结果不起作用?
Posted
技术标签:
【中文标题】在 Swift 3 中过滤 UICollectionview 上的结果不起作用?【英文标题】:Filtering the results on a UICollectionview in Swift 3 not working? 【发布时间】:2017-06-23 09:53:19 【问题描述】:第一次加载 itemlist 工作正常,但过滤后集合视图没有更新。我添加了一个搜索栏并尝试过滤结果,但 UICollectionview 无法更新视图。
var resultSearchController = UISearchController()
//itemlist
var itemList = [item1, item2, item3, item4,..]
//configured searchbar
func viewDidLoad()
self.resultSearchController = UISearchController(searchResultsController: nil)
self.resultSearchController.searchResultsUpdater = self
self.resultSearchController.searchBar.delegate = self
self.definesPresentationContext = true
self.resultSearchController.dimsBackgroundDuringPresentation = false
self.resultSearchController.searchBar.placeholder = "Search for Items"
self.resultSearchController.searchBar.searchBarStyle = UISearchBarStyle.prominent
self.resultSearchController.searchBar.sizeToFit()
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
if resultSearchController.isActive
return filterSearch.count
return itemList.count
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
var item = itemList()
if resultSearchController.isActive
item = filterSearch[indexPath.row]
else
item = itemList[indexPath.row]
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellIdentifier", for: indexPath)
cell.titleLabel.text = item
return cell
//将项目过滤到filterSearch但集合视图没有根据过滤器更新。
func updateSearchResults(for searchController: UISearchController)
filterSearch = itemList.filter itemList in
return temList.name.lowercased().contains(searchText.lowercased())
self.collectionView?.reloadData()
【问题讨论】:
你能在你的代码中提供更多关于“storeList”的代码吗? 通过调试我看到searchcontroller虽然处于活动状态,但并未显示为活动状态。你能告诉我我犯的错误吗?@PhaniSai 您的代码看起来不错,但是您可以在返回“true”时检查“resultSearchController.isActive” 在激活编辑时,resultSearchController.isActive 返回“true”,但在编辑时返回 false。你能解释一下吗? @PhaniSai 我刚刚更新了答案看看变化 【参考方案1】:检查以下代码中的更改
var resultSearchController = UISearchController()
//itemlist
var itemList = [item1, item2, item3, item4,..]
//configured searchbar
var issearchactive:Bool = false
func viewDidLoad()
self.resultSearchController = UISearchController(searchResultsController: nil)
self.resultSearchController.searchResultsUpdater = self
self.resultSearchController.searchBar.delegate = self
self.definesPresentationContext = true
self.resultSearchController.dimsBackgroundDuringPresentation = false
self.resultSearchController.searchBar.placeholder = "Search for Items"
self.resultSearchController.searchBar.searchBarStyle = UISearchBarStyle.prominent
self.resultSearchController.searchBar.sizeToFit()
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
if issearchactive
return filterSearch.count
return itemList.count
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
var item = itemList()
if issearchactive
item = filterSearch[indexPath.row]
else
item = itemList[indexPath.row]
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellIdentifier", for: indexPath)
cell.titleLabel.text = item
return cell
func updateSearchResults(searchText:String)
filterSearch = itemList.filter obj in
return obj.name.lowercased().contains(searchText.lowercased())
self.collectionView?.reloadData()
override func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool
issearchactive = true
return true
override func textFieldDidEndEditing(_ textField: UITextField)
updateSearchResults(searchText:textfield.text)
issearchactive = false
override func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool
updateSearchResults(searchText:textfield.text)
return true
【讨论】:
以上是关于在 Swift 3 中过滤 UICollectionview 上的结果不起作用?的主要内容,如果未能解决你的问题,请参考以下文章
在 Swift 3 中过滤 UICollectionview 上的结果不起作用?
如何在swift 3中过滤UISearchBar中的数组模型