L'ajout d'une barre de recherche par programme pour tableview dans swift

J'ai un textfield qui représente une tableview comme son inputview. Je veux ajouter 2 choses à ce tableview.

1) ajouter une barre de recherche.
2) ajouter annuler bouton au haut de la tableview.

class enterYourDealVC: UIViewController, UITableViewDataSource, UITableViewDelegate, UISearchBarDelegate, UISearchDisplayDelegate, UISearchResultsUpdating {
var tableView: UITableView  =   UITableView()
let searchController = UISearchController(searchResultsController: nil)

var dealAirports = [
    airPorts(name: "Airport1", shortcut: "AP1")!),
    airPorts(name: "Airport2", shortcut: "AP2")!)
]
var filteredAirports = [airPorts]()


//view did load
    tableView = UITableView(frame: UIScreen.mainScreen().bounds, style: UITableViewStyle.Plain)
    tableView.delegate      =   self
    tableView.dataSource    =   self
    tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")

    searchController.searchResultsUpdater = self
    searchController.dimsBackgroundDuringPresentation = false
    definesPresentationContext = true
    tableView.tableHeaderView = searchController.searchBar
    toTextField.inputView = self.tableView

//here is my search function
func filterContentForSearchText(searchText: String, scope: String = "All") {
    filteredAirports = dealAirports.filter { ap in
        return ap.name.lowercaseString.containsString(searchText.lowercaseString)
    }

    tableView.reloadData()
}
}

Le problème avec ce code, il ne fait pas de recherche. Aussi, quand je clique sur la barre de recherche il rejeter le tableview et me renvoie dos à viewcontroller. Comment puis-je résoudre ce problème?

et comment puis-je ajouter un bouton annuler pour cette tableview?

UISearchController Apple exemple de code : Apple exemple de code Ce sera vous aider à mettre en œuvre.
Il pourrait m'aider si je savais objective-c.. Il est très compliqué pour moi. Je vais continuer à l'examen du code.

OriginalL'auteur Berk Kaya | 2016-07-21