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

时间:2018-04-22 00:36:33

标签: swift uicollectionview uicollectionviewcell uisearchbar uisearchcontroller

我用搜索栏创建了一个集合视图。可以在此集合视图中选择行。当我搜索某些内容并选择一行时,搜索栏工作正常。但是,当我单击搜索栏的取消按钮时,所选行正在更改。例如,集合视图中有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 个答案:

答案 0 :(得分:0)

  1. 好的,你有3行(“a”,“b”,“c”)
  2. 然后搜索“c”
  3. 现在,您在表格中有一行“c”
  4. 此行包含Indexpath(section = 0,row = 0)
  5. 现在您选择具有Indexpath(section = 0,row = 0)
  6. 的行
  7. 取消搜索 - 再次3行
  8. 所选行是Indexpath(section = 0,row = 0)的行。猜猜这是什么项目? (“a”)
  9. 我认为您应该存储选定的项目而不是索引路径并在cellforrow方法中选择单元格