Swift 取消搜索栏时,所选行正在更改

Swift 取消搜索栏时,所选行正在更改,swift,uicollectionview,uicollectionviewcell,uisearchbar,uisearchcontroller,Swift,Uicollectionview,Uicollectionviewcell,Uisearchbar,Uisearchcontroller,我用搜索栏创建了一个集合视图。在此集合视图上可以选择行。当我搜索某物和选择一行时,搜索栏工作正常。但是,当我单击搜索栏的“取消”按钮时,所选行正在更改。例如,集合视图上有3行。它们都是可选的。我正在搜索第三排并选择它。选择第三行后,如果单击搜索栏的“取消”按钮,则所选行正在更改,而第一行正在被选中。我如何处理这个问题 import UIKit class CollectionViewController: UIViewController, UICollectionViewDelegate,

我用搜索栏创建了一个集合视图。在此集合视图上可以选择行。当我搜索某物和选择一行时,搜索栏工作正常。但是,当我单击搜索栏的“取消”按钮时,所选行正在更改。例如,集合视图上有3行。它们都是可选的。我正在搜索第三排并选择它。选择第三行后,如果单击搜索栏的“取消”按钮,则所选行正在更改,而第一行正在被选中。我如何处理这个问题

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!)
}
 }
  • 好的,您有3行(“a”、“b”、“c”)
  • 然后搜索“c”
  • 现在表中有1行带有“c”
  • 此行具有Indexpath(节=0,行=0)
  • 现在,使用Indexpath选择行(section=0,row=0)
  • 取消搜索-再次搜索3行
  • 所选行是具有Indexpath的行(section=0,row=0)。猜猜这是什么东西?(“a”)
  • 我想您应该存储选定的项而不是IndExpath,并在cellforrow方法中选择单元格