swift - UiSearchController 到 Swift 中的 UiCollectionView

标签 swift xcode uicollectionview uicollectionviewcell uisearchcontroller

我想以编程方式添加搜索栏。我希望它看起来像在设置应用程序中(向下滚动时出现,向上滚动时消失)。

但是搜索栏没有出现。

大纲: enter image description here

View Controller :

import UIKit

class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UISearchControllerDelegate, UISearchBarDelegate, UISearchResultsUpdating {

    var filtered:[String] = []
    var searchActive : Bool = false
    let searchController = UISearchController(searchResultsController: nil)

    @IBOutlet weak var collectionView: UICollectionView!

    var items = ["Apple", "Orange", "Apple", "Orange", "Apple", "Orange", "Apple", "Orange", "Apple", "Orange", "Apple", "Orange"]

    override func viewDidLoad() {
        super.viewDidLoad()

        collectionView.delegate = self
        collectionView.dataSource = self

        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..."
        searchController.searchBar.sizeToFit()
        searchController.searchBar.becomeFirstResponder()
        self.navigationItem.titleView = searchController.searchBar

        self.definesPresentationContext = true
        self.searchController.searchBar.placeholder = "Search for Items"

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

            if searchActive {
                return filtered.count
            }
            else
            {
                return items.count
            }
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CollectionViewCell

        cell.contentView.layer.cornerRadius = 7.0
        cell.contentView.layer.borderWidth = 1.0
        cell.contentView.layer.borderColor = UIColor.clear.cgColor
        cell.contentView.layer.masksToBounds = false
        cell.layer.cornerRadius = 7.0

        return cell
    }

    func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
        searchActive = false
        self.dismiss(animated: true, completion: nil)
    }

    func updateSearchResults(for searchController: UISearchController)
    {
        let searchString = searchController.searchBar.text

        filtered = items.filter({ (item) -> Bool in
            let countryText: NSString = item as NSString

            return (countryText.range(of: searchString!, options: NSString.CompareOptions.caseInsensitive).location) != NSNotFound
        })

        collectionView.reloadData()
    }

    func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
        searchActive = true
        collectionView.reloadData()
    }

    func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
        searchActive = false
        collectionView.reloadData()
    }

    func searchBarBookmarkButtonClicked(_ searchBar: UISearchBar) {
        if !searchActive {
            searchActive = true
            collectionView.reloadData()
        }

        searchController.searchBar.resignFirstResponder()
    }
}

应用程序正在运行,显示项目,但滚动时没有搜索栏。 也许这是因为我使用的是 UiCollectionView 而不是 UiTableView,我必须添加一些额外的代码? 我错过了什么?

视频,外观:https://drive.google.com/file/d/1P0carjjgiForBnK_UmiuBWWk5A7BqSWB/view?usp=sharing

最佳答案

您运行的是 iOS 11 吗?您是否下拉了 Collection View ?搜索栏仅在下拉时出现。我看到的与我正在工作的唯一区别是 navigationItem searchcontroller:

self.navigationItem.searchcontroller = searchcontroller

关于swift - UiSearchController 到 Swift 中的 UiCollectionView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52414013/

相关文章:

ios - 将 iAd 和 Admob 集成国家添加到 App Store

ios - 什么定义了 UITableViewCell 中 CollectionViewCell 的大小?

ios - [UICollectionView setCollectionViewLayout :animated:] 上的错误访问

Swift ObjectMapper - 数组映射

swift - 警告 : No screenshots were found for '( locale )' when trying to -exportLocalizations

swift - Xcode 11 中没有更多上下文的表达式类型不明确

ios - 从后台返回应用程序时未调用自定义委托(delegate)方法

swift - UIImageView 无法正确显示图像

ios - 上下文类型 'NSFastEnumeration' 不能与数组文字一起使用

swift - 导航栏在我第二次打开 viewController 时隐藏(SWRevealViewController 中)