我对Xcode和Swift非常陌生,目前正在从事学校项目。我已经将UISearchBar链接到UITableView并能够正确搜索。但是,我遇到的问题是,当我使用搜索栏单击某个项目时,链接到该项目的图像不会在下一页上弹出。当我不搜索项目并在表格视图中单击右键时,它会很好地工作。这是我的代码。
@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 { $0.range(of: searchText, options: .caseInsensitive) != nil }
searching = true
tblView.reloadData()
}
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
searching = false
searchBar.text = ""
tblView.reloadData()
}
}
`
答案 0 :(得分:0)
问题是您的数据源:学校名称vs搜索学校。在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)
}