gpt4 book ai didi

ios - CollectionView中的程序化搜索栏实现问题

转载 作者:行者123 更新时间:2023-11-30 12:15:35 25 4
gpt4 key购买 nike

我以编程方式实现了我的搜索栏。对于 TableView 中的默认行为,我设法使其在标题 View 中工作。现在,我正在 collectionView 中尝试相同的实现,但似乎无法使其工作。任何帮助都值得赞赏,因为我已经挣扎了好几天了。我将发布整个 View Controller ,以防我遗漏某些内容:

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!)
}
}

最佳答案

我找到了解决方案,而且非常简单。我放置了一个 View 作为支持,然后将 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()
}
}
}

关于ios - CollectionView中的程序化搜索栏实现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45471125/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com