我对老师的应用程序进行了评分。因此,在第一个屏幕上,我有一个主题,例如数学,物理,法语等,当您单击该主题时,在第二个屏幕上会为您提供该主题的教师列表及其名称。我的问题是,我想在第二个视图上实现一个searchController
,该视图带有教师姓名列表。我想能够在searchBar上搜索一位老师的名字,但我什么也不能返回。
这是我的第二个屏幕控制器的代码,其中包含我的searchController
import UIKit
extension ChooseTeachersViewController: UISearchResultsUpdating {
func updateSearchResults(for searchController: UISearchController) {
filterContentForSearchText(searchController.searchBar.text!)
}
}
class ChooseTeachersViewController: UITableViewController {
var listOfSubject: listOfSubjects!
var list: [listOfSubjects] = []
var listTeacher: [listOfTeachers] = [] // new code
let searchController = UISearchController(searchResultsController: nil)
var filteredListOfTeacher = [listOfTeachers]()// filtered list for the teacher when using the searchBar
func searchBarIsEmpty() -> Bool {
//return true if the text is empty
return searchController.searchBar.text?.isEmpty ?? true
}
func isFiltering() -> Bool {
return searchController.isActive && !searchBarIsEmpty()
}
func filterContentForSearchText(_ searchText: String, scope: String = "All"){
filteredListOfTeacher = filteredListOfTeacher.filter({( listTeacher : listOfTeachers) -> Bool in
return listTeacher.nameOfteacher.lowercased().contains(searchText.lowercased())
})
tableView.reloadData()
}
override func viewDidLoad() {
super.viewDidLoad()
let titlePage: String = "Select a \(listOfSubject.nameOfSubject) teacher"
self.navigationItem.title = titlePage // new code
// Do any additional setup after loading the view, typically from a nib.
//searchbar UI
searchController.searchResultsUpdater = self
searchController.obscuresBackgroundDuringPresentation = false
searchController.searchBar.placeholder = "Search a \(listOfSubject.nameOfSubject) teacher"
navigationItem.searchController = searchController
definesPresentationContext = true
//tableView.tableHeaderView = searchController.searchBar
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// return the list of item that was filered by the seachBar
if isFiltering() {
return listOfSubject.filteredListOfTeacher.count
}
return listOfSubject.teachers.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "course", for: indexPath)
let listTeachers: listOfTeachers
if isFiltering() {
listTeachers = listOfSubject.filteredListOfTeacher[indexPath.row]
} else {
listTeachers = listOfSubject.teachers[indexPath.row]
}
//let teacher = listOfSubject.teachers[indexPath.row]
configureSubject(for: cell, with: listTeachers)
return cell
}
//method that sets the subject item text on the cell label
func configureSubject(for cell: UITableViewCell, with subject: listOfTeachers){
let label = cell.viewWithTag(1000) as! UILabel
label.text = subject.nameOfteacher
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let listOfTeacher = listOfSubject.teachers[indexPath.row] // new code
performSegue(withIdentifier: "showTeacherPage", sender: listOfTeacher)
}
//segue not connected to teacherPageViewController;
//TODO make the connection when the add comment is fixed.
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showTeacherPage" {
let controller = segue.destination as! teacherPageViewController
controller.listOfTeacher = sender as? listOfTeachers
}
}
}