SWIFT 4 UISearchBar 未加载图像

SWIFT 4 UISearchBar Not Loading Images

我对 Xcode 和 swift 非常陌生,目前正在做一个学校项目。我已将 UISearchBar 链接到 UITableView 并且能够正确搜索。但是,我遇到的问题是当我使用搜索栏单击某个项目时,链接到该项目的图像不会在下一页上弹出。当我不搜索该项目并从 table 视图中右击时,它运行良好。这是我的代码。

@IBOutlet weak var schoolSearch: UISearchBar!
@IBOutlet weak var tblView: UITableView!


let schoolnames = ["Long Beach City College LAC", "California State University, Bakersfield", "California State University, Cal Maritime", "California State University, Channel Islands", "California State University, Chico", "California State University, Dominguez Hills", "California State University, East Bay", "California State University, Fresno", "California State University, Fullerton", "California State University, Humboldt", "California State University, Long Beach", "California State University, Los Angeles", "California State University, Monterey Bay", "California State University, Northridge", "California State Polytechnic University, Pomona", "California State University, Sacramento", "California State University, San Bernardino", "California State University, San Diego", "California State University, San Francisco", "California State University, San Jose", "California Polytechnic State University, San Luis Obispo", "California State University, San Marcos", "California State University, Sonoma", "California State University, Stanislaus", "University of California Berkeley", "University of California Davis", "University of California Irvine", "University of California Los Angeles", "University of California Merced", "University of California Riverside", "University of California San Diego", "University of California San Francisco",  "University of California Santa Barbara", "University of California Santa Cruz", "Long Beach City College PCC", "El Camino College Torrance", "El Camino College Compton", "Cerritos College", "Cypress College"]

var searchedSchool = [String]()
var searching = false

override func viewDidLoad() {
    super.viewDidLoad()
    schoolSearch.delegate = self

    self.tblView.delegate = self
    self.tblView.reloadData()
    // Do any additional setup after loading the view.
}

/*
// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    // Get the new view controller using segue.destination.
    // Pass the selected object to the new view controller.
}
*/
}
extension ChooseSchool: UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate {

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if searching {
        return searchedSchool.count
    } else {
        return schoolnames.count
    }
}

 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
      let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as? TableViewCell
      cell?.img.image = UIImage(named: schoolnames[indexPath.row])
      cell?.lbl.text = schoolnames[indexPath.row]

      _ = tableView.dequeueReusableCell(withIdentifier: "cell")
      if searching {
           cell?.textLabel?.text = searchedSchool[indexPath.row]
      } else{
           cell?.textLabel?.text = schoolnames[indexPath.row]
      }
      return cell!
 }

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let vc = storyboard?.instantiateViewController(withIdentifier: "TestController") as? TestController
 vc?.schoolnames = schoolnames[indexPath.row]
    navigationController?.pushViewController(vc!, animated: true)
}

func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
 searchedSchool = schoolnames.filter { [=11=].range(of: searchText, options: .caseInsensitive) != nil }
    searching = true
    tblView.reloadData()
}

func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
    searching = false
    searchBar.text = ""
    tblView.reloadData()
}
}

`

问题在于您的数据源:schoolnames 与 searchedSchool。在你 didSelectRowAt 你必须设置条件。

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let vc = storyboard?.instantiateViewController(withIdentifier: "TestController") as? TestController
if searching {
vc?.schoolnames = searchedSchool[indexPath.row]
} else {
 vc?.schoolnames = schoolnames[indexPath.row]
}
    navigationController?.pushViewController(vc!, animated: true)
}