Ios 创建可重用的SearchController

Ios 创建可重用的SearchController,ios,swift,xcode,uisearchcontroller,Ios,Swift,Xcode,Uisearchcontroller,是否可以创建UISearchController以便在许多类中重复使用 在新类中使用最少的代码 现在我在所有类中都有代码,但我想在所有类中使用最少的代码来调用searchController: class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate { @IBOutlet weak var tableView: UITableView!

是否可以创建UISearchController以便在许多类中重复使用

在新类中使用最少的代码

现在我在所有类中都有代码,但我想在所有类中使用最少的代码来调用
searchController

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate {

    @IBOutlet weak var tableView: UITableView!

    var photoBook: [PhotoBooking] = []
    var filteredBook: [PhotoBooking] = []

    private var searchController = UISearchController(searchResultsController: nil)
    private var searchBarIsEmpty: Bool {
        guard let text = searchController.searchBar.text else { return false }
        return text.isEmpty
    }

    private var isFiltering: Bool {
        return searchController.isActive && !searchBarIsEmpty
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        configureSearchController()
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if isFiltering {
            return filteredBook.count
        }
        return photoBook.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
        var pBook: PhotoBooking
        if isFiltering {
            pBook = filteredBook[indexPath.row]
        } else {
            pBook = photoBook[indexPath.row]
        }
        cell.textLabel?.text = pBook.invoiceID
        cell.detailTextLabel?.text = pBook.contactInfo["surname"] as! String
        return cell
    }

    private func configureSearchController() {
        searchController = UISearchController(searchResultsController: nil)
        searchController.searchResultsUpdater = self
        searchController.searchBar.barTintColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
        searchController.dimsBackgroundDuringPresentation = false
        searchController.searchBar.delegate = self
        tableView.tableHeaderView = searchController.searchBar
        definesPresentationContext = true
    }
}

extension AllPhotoBookingsViewController: UISearchResultsUpdating {
    func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
        filterContentForSearchText(searchBar.text!)
        tableView.reloadData()
    }

    func updateSearchResults(for searchController: UISearchController) {
        filterContentForSearchText(searchController.searchBar.text!)
        tableView.reloadData()
    }

    private func filterContentForSearchText(_ searchText: String) {
        filteredBook = photoBook.filter({ (book: PhotoBooking) -> Bool in
            let name = book.contactInfo["name"] as! String
            let surname = book.contactInfo["surname"] as! String
            let invoice = book.invoiceID
            return invoice.localizedCaseInsensitiveContains(searchText) ||
                name.localizedCaseInsensitiveContains(searchText) ||
                surname.localizedCaseInsensitiveContains(searchText)
        })
        tableView.reloadData()
    }

    func searchBarTextDidEndEditing(_ searchBar: UISearchBar) {
        navigationController?.hidesBarsOnSwipe = false
    }
}

可能需要使用
协议
,但我不知道如何正确使用协议。

有很多方法,使用协议是一种很好的方法,但是,您也可以将
UIViewController
子类化,并制作一个
BaseSearchableViewController
,在其中添加搜索所需的所有功能,然后,您只需从
BaseSearchableViewController
子类化,而不是
UIViewController
,我想这会对您有所帮助(定制您的
UISearchController
并准备重用)我给了您一种不用协议的方法,我想我无法在这个答案中解释协议的全部功能。