取消搜索栏时,所选行正在更改
Posted
技术标签:
【中文标题】取消搜索栏时,所选行正在更改【英文标题】:When searchbar is cancelled, selected row is changing 【发布时间】:2018-04-22 00:36:33 【问题描述】:我创建了一个带有搜索栏的集合视图。在此集合视图上可以选择行。当我搜索某些内容和选择一行时,搜索栏工作得很好。但是,当我单击搜索栏的取消按钮时,所选行正在更改。例如,集合视图上有 3 行。所有这些都是可选的。我正在寻找第 3 行并选择它。选择第 3 行后,如果我单击搜索栏的取消按钮,则所选行正在更改,并且第 1 行正在被选中。我该如何处理这个问题?
import UIKit
class CollectionViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UISearchControllerDelegate, UISearchBarDelegate
@IBOutlet weak var collectionView: UICollectionView!
let searchController = UISearchController(searchResultsController: nil)
var things = [Things]()
var filteredThings = [Things]()
override func viewDidLoad()
super.viewDidLoad()
collectionView.delegate = self
collectionView.dataSource = self
CollectionViewController.instance = self
things = [
Things(name: "1", imageName: "firstImage", including: false),
Things(name: "2", imageName: "secondImage", including: false),
Things(name: "3", imageName: "thirdImage", including: false)
]
// Setup the Search Controller
self.searchController.searchResultsUpdater = self
self.searchController.delegate = self
self.searchController.searchBar.delegate = self
self.searchController.hidesNavigationBarDuringPresentation = false
self.searchController.dimsBackgroundDuringPresentation = true
self.searchController.obscuresBackgroundDuringPresentation = false
searchController.searchBar.placeholder = "Search for tools and resources"
searchController.searchBar.sizeToFit()
searchController.searchBar.becomeFirstResponder()
self.navigationItem.titleView = searchController.searchBar
definesPresentationContext = true
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
if isFiltering()
return filteredThings.count
return things.count
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "customCell", for: indexPath) as! CustomCollectionViewCell
let thing: Thing
if isFiltering()
thing = filteredThings[indexPath.row]
else
thing = things[indexPath.row]
cell.imageView.image = UIImage(named: thing.imageName)
cell.labelView.text = thing.name
cell.layer.cornerRadius = 7
cell.imageView.layer.cornerRadius = 7
return cell
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath)
let cell = collectionView.cellForItem(at: indexPath)
var thing: Things
if isFiltering()
thing = filteredThings[indexPath.row]
else
thing = things[indexPath.row]
cell?.layer.cornerRadius = 5
cell?.layer.borderWidth = 3
cell?.layer.borderColor = myGreenTabBarColor.cgColor
thing.including = true
print(thing.name)
print(thing.including)
collectionView.allowsMultipleSelection = true
print("This cell is selected")
func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath)
let cell = collectionView.cellForItem(at: indexPath)
var thing: Things
if isFiltering()
thing = filteredThings[indexPath.row]
else
thing = things[indexPath.row]
cell?.layer.cornerRadius = 5
cell?.layer.borderWidth = 3
cell?.layer.borderColor = UIColor.white.cgColor
thing.including = false
print(thing.including)
collectionView.allowsMultipleSelection = true
print("This cell is Deselected")
func searchBarIsEmpty() -> Bool
// Returns true if the text is empty or nil
return searchController.searchBar.text?.isEmpty ?? true
func searchBarCancelButtonClicked(_ searchBar: UISearchBar)
self.dismiss(animated: true, completion: nil)
collectionView.reloadData()
func filterContentForSearchText(_ searchText: String, scope: String = "All")
filteredThings = things.filter(( thing : Things) -> Bool in
return thing.name.lowercased().contains(searchText.lowercased())
)
collectionView.reloadData()
func isFiltering() -> Bool
return searchController.isActive && !searchBarIsEmpty()
extension CollectionViewController: UISearchResultsUpdating
// MARK: - UISearchResultsUpdating Delegate
func updateSearchResults(for searchController: UISearchController)
// TODO
filterContentForSearchText(searchController.searchBar.text!)
【问题讨论】:
【参考方案1】:-
好的,你有 3 行(“a”、“b”、“c”)
然后你搜索“c”
现在表格中有 1 行带有“c”
这一行有 Indexpath(section = 0, row = 0)
现在您使用 Indexpath(section = 0, row = 0) 选择行
取消搜索 - 再次搜索 3 行
并且选中的行是 Indexpath(section = 0, row = 0) 的行。猜猜这是什么单品? (“一”)
我想你应该存储选定的项目而不是索引路径并在 cellforrow 方法中选择单元格
【讨论】:
以上是关于取消搜索栏时,所选行正在更改的主要内容,如果未能解决你的问题,请参考以下文章
PrimeReact DataTable:如何(视觉)取消选择行