繁体   English   中英

集合视图中的程序化搜索栏实现问题

[英]Programatic search bar in collection view implementation problems

我以编程方式实现了搜索栏。 对于表视图中的默认行为,我设法使其在标题视图中起作用。 现在,我正在collectionView中尝试相同的实现,但似乎无法使其正常工作。 由于我已经奋斗了好几天,所以对它的任何帮助都表示赞赏。 我发布了整个视图控制器,以防万一我丢失了一些东西:

import UIKit
import Firebase
import FirebaseDatabase

class NHSTrustSearchData: UIViewController, UICollectionViewDelegateFlowLayout, UICollectionViewDataSource, UISearchBarDelegate {

    var setupNHSTSD = SetupNHSTSD()

    var nhsTrusts = [NhsTrust]()
    var nhsTrustsFiltered = [NhsTrust]()

    let searchController = UISearchController(searchResultsController: nil)

    override func viewDidLoad() {
        super.viewDidLoad()

        // Initialize the window
        let window = UIWindow.init(frame: UIScreen.main.bounds)
        window.makeKeyAndVisible()

        setupNHSTSD.nhsTrustSearchData = self
        setupNHSTSD.setupViews()

        setupNHSTSD.collectioViewSetup()

        setupBasicNavigationForSocialHealthcare()

        setupNHSTSD.collectionView.dataSource = self as UICollectionViewDataSource
        setupNHSTSD.collectionView.delegate = self

        setupNHSTSD.collectionView.register(NHSTrustCell.self, forCellWithReuseIdentifier: "NHSTustCell")

        setupNHSTSD.searchBar.delegate = self

        navigationItem.title = "Choose NHS Trust"
        print("User role remains: \(currentUserRole)")

        fetchQuestions()

        searchController.searchResultsUpdater = self as UISearchResultsUpdating
        searchController.dimsBackgroundDuringPresentation = false
        definesPresentationContext = true
        setupNHSTSD.searchBar = searchController.searchBar

    }

    func filterContentForSearchText(searchText: String, scope: String = "All") {
        nhsTrustsFiltered = nhsTrusts.filter { trust in
            return trust.nhsTrustName.localizedLowercase.contains(searchText.localizedLowercase)

        }

        setupNHSTSD.collectionView.reloadData()
    }


    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        if searchController.isActive && searchController.searchBar.text != "" {
            return nhsTrustsFiltered.count
        }
        return nhsTrusts.count
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "NHSTustCell", for: indexPath) as! NHSTrustCell
        cell.backgroundColor = .white
        cell.layer.borderColor = UIColor(white: 0.5, alpha: 0.3).cgColor
        cell.layer.borderWidth = 0.3

        let trust : NhsTrust
        if searchController.isActive && searchController.searchBar.text != "" {
            trust = nhsTrustsFiltered[indexPath.row]
        } else {
            trust = nhsTrusts[indexPath.row]
        }
        cell.nhsTrustName.text = trust.nhsTrustName
        return cell

    }

}

extension NHSTrustSearchData: UISearchResultsUpdating {
    @available(iOS 8.0, *)
    func updateSearchResults(for searchController: UISearchController) {
        filterContentForSearchText(searchText: searchController.searchBar.text!)
    }

    func updateSearchResultsForSearchController(searchController: UISearchController) {
        filterContentForSearchText(searchText: searchController.searchBar.text!)
    }
}

我找到了解决方案,而且非常简单。 我放置了一个视图作为支持,并刚刚将searchController添加为subView:

setupNHSTSD.viewForSearch.addSubview(searchController.searchBar)

如果要取出searchBar的背景图像,可以使用:

//remove searchBar background image
    for subView in searchController.searchBar.subviews {
        for view in subView.subviews {

            if view.isKind(of: NSClassFromString("UISearchBarBackground")!) {
                let imageView = view as! UIImageView
                imageView.removeFromSuperview()
            }
        }
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM