Swift Search Result Controller in search results segue to another view controller

不打扰是莪最后的温柔 提交于 2019-12-04 14:57:48

First create a protocol

protocol SelectedCellProtocol {
    func didSelectedCell(text: String)
}

on your UITableViewClass declare it

class MySearchResultsController: UITableViewController, UISearchResultsUpdating {
    var delegate:SelectedCellProtocol?
}

and on the selected cell method call it like :

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath){
    self.delegate?.didSelectedCell(cell.textLabel?.text)
}

when you declare your results controller, set the delegate

let resultsController = MySearchResultsController()
    resultsController.databaseFilePath = databaseFilePath()
    //this is essential that I use a segue because between my views I'm passing information between them.
    resultsController.photo = photo!
resultsController.delegate = self

    searchController = UISearchController(searchResultsController:       resultsController)
    let searchBar = searchController.searchBar
    searchBar.placeholder = searchBarPlaceHolderText
    searchBar.sizeToFit()
    tableView.tableHeaderView = searchBar
    searchController.searchResultsUpdater = resultsController

and then implement the protocol

class MyNavTableViewController: UIViewController, UITableViewDataSource, SelectedCellProtocol{
    func didSelectedCell(text: String) {
        print(text)
        //make your custom segue
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!