【问题标题】:Adding a search bar to ui table view on overlay将搜索栏添加到覆盖上的 ui 表视图
【发布时间】:2019-06-05 21:11:28
【问题描述】:

我已经设法创建了一个深色覆盖,一旦单击了搜索图标,并设法在深色覆盖上添加了一个表格 uiview,但现在想在该表格视图中添加一个搜索栏。我似乎无法弄清楚,因为我的代码似乎与其他所有人的示例不同。我是 swift 新手,所以我的代码可能不是最干净的。我可以问是否有人可以告诉我如何做到这一点,因为我没有想法。我已经在下面发布了我的代码,有人可以告诉我哪里出错了。

非常感谢



class SearchLauncher: NSObject {

    let blackView = UIView()


    let tableView = UITableView()



    @objc func showSearch() {

        if let window = UIApplication.shared.keyWindow {

            blackView.backgroundColor = UIColor(white: 0, alpha: 0.5)

            blackView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(handleDismiss)))

            window.addSubview(blackView)

            window.addSubview(tableView)


            let height: CGFloat = 600
            let y = window.frame.height - height
            tableView.frame = CGRect(x: 0, y: window.frame.height, width: window.frame.width, height: height)

            blackView.frame = window.frame
            blackView.alpha = 0

            UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseOut, animations: {

                self.blackView.alpha = 1

                self.tableView.frame = CGRect(x: 0, y: y, width: self.tableView.frame.width, height: self.tableView.frame.height)

            }, completion: nil)
        }



    }

    @objc func handleDismiss() {

        UIView.animate(withDuration: 0.5) {
            self.blackView.alpha = 0

            if let window = UIApplication.shared.keyWindow {
                self.tableView.frame = CGRect(x: 0, y: window.frame.height, width: self.tableView.frame.width, height: self.tableView.frame.height)
            }
        }


    }



    override init() {
        super.init()
        //start doing something here maybe

    }



}```

【问题讨论】:

    标签: swift uiview uisearchbar


    【解决方案1】:

    您是如何尝试添加搜索控制器的?

    我会通过添加一个 UISearchController 作为表视图的 tableHeaderView 来做到这一点。首先确保将UISearchResultsUpdating 协议添加到您的类中。

    class SearchLauncher: NSObject, UISearchResultsUpdating
    

    然后将搜索栏添加到您的表格视图中

    let searchController = UISearchController(searchResultsController: nil)
    searchController.searchBar.sizeToFit()        
    searchController.searchResultsUpdater = self
    
    //you probably don't need this
    //searchController.dimsBackgroundDuringPresentation = false
    
    tableView.tableHeaderView = searchController.searchBar
    

    然后,实现 updateSearchResults(for:_) 委托方法

    func updateSearchResults(for searchController: UISearchController) {
        filteredTableData.removeAll(keepingCapacity: false)
    
        //filter the table data by using searchController.searchBar.text!
        //filteredTableData = ...
    
        self.tableView.reloadData()
    }
    

    【讨论】:

    • 非常感谢,我一直在尝试的所有方式都是我只是关注 youtube 视频,但我似乎无法在我的代码中实现它们。现在让我尝试定制设计。非常感谢,谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-28
    相关资源
    最近更新 更多