ios - 过滤(搜索表)后,项目选择不起作用(Swift)
问题描述
import UIKit
let country = ["Argentina", "Australia", "Belgium", "Brazil", "Colombia", "Costa Rica", "Croatia", "Denmark", "Egypt", "England", "France", "Germany", "Iceland", "Iran", "Japan", "Mexico", "Morocco", "Nigeria", "Panama", "Peru", "Poland", "Portugal", "Republic of Korea", "Russia", "Saudi Arabia", "Senegal", "Serbia", "Spain", "Sweden", "Switzerland", "Tunis", "Uruguay"]
let country2 = ["Argentina2", "Australia2", "Belgium2", "Brazil2", "Colombia2", "Costa Rica2", "Croatia2", "Denmark2", "Egypt2", "England2", "France2", "Germany2", "Iceland2", "Iran2", "Japan2", "Mexico2", "Morocco2", "Nigeria2", "Panama2", "Peru2", "Poland2", "Portugal2", "Republic of Korea2", "Russia2", "Saudi Arabia2", "Senegal2", "Serbia2", "Spain2", "Sweden2", "Switzerland2", "Tunis2", "Uruguay2"]
let country3 = ["Argentina3", "Australia3", "Belgium3", "Brazil3", "Colombia3", "Costa Rica3", "Croatia3", "Denmark3", "Egypt3", "England3", "France3", "Germany3", "Iceland3", "Iran3", "Japan3", "Mexico3", "Morocco3", "Nigeria3", "Panama3", "Peru3", "Poland3", "Portugal3", "Republic of Korea3", "Russia3", "Saudi Arabia3", "Senegal3", "Serbia3", "Spain3", "Sweden3", "Switzerland3", "Tunis3", "Uruguay3"]
var myIndex = 0
class TableViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate {
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
filteredArray = country.filter({$0.lowercased().contains(searchText.lowercased())})
if searchText == "" {
filteredArray = country
}
self.tableView.reloadData()
}
var filteredArray = [String]()
var searchController = UISearchController()
var searchBar = UISearchBar()
var resultController = UITableViewController()
let country = ["Argentina", "Australia", "Belgium", "Brazil", "Colombia", "Costa Rica", "Croatia", "Denmark", "Egypt", "England", "France", "Germany", "Iceland", "Iran", "Japan", "Mexico", "Morocco", "Nigeria", "Panama", "Peru", "Poland", "Portugal", "Republic of Korea", "Russia", "Saudi Arabia", "Senegal", "Serbia", "Spain", "Sweden", "Switzerland", "Tunis", "Uruguay"]
@IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
tableView.dataSource = self
tableView.delegate = self
filteredArray = country
searchBar.frame = CGRect(x: 0, y: 0, width: 400, height: 50)
self.tableView.tableHeaderView = searchBar
self.searchBar.delegate = self
definesPresentationContext = true
}
// MARK: - Table view data source
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.filteredArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifier", for: indexPath)
cell.accessoryType = .disclosureIndicator
cell.textLabel?.font = UIFont.systemFont(ofSize: 18.0)
cell.textLabel?.text = self.filteredArray[indexPath.row]
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
performSegue(withIdentifier: "seque", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "seque" {
if let indexPath = self.tableView.indexPathForSelectedRow {
let viewController: ViewController = segue.destination as! ViewController
viewController.myIndex = indexPath
}
}
}
}
解决方案
您的代码中存在一些问题:
- 在
cellForRow
您使您的表视图出队时,而不是委托方法给出的表视图。self.tableView.dequeueReusableCell(withIdentifier:
这里不要使用self
. - 您正在使用
resultController
没有任何原型单元格来显示数据。在这里,您的应用程序将崩溃。 - 我不明白你做了什么
prepareForSegue
- 不要使用任何全局变量。如果要使用
myIndex
,请在destinationVC 中创建一个属性并传递它prepareForSegue
。 - 您在表格视图和选择从数组中获取值时显示过滤
country
数组,这就是您得到错误值的原因。
我发现,您不需要 a SearchViewController
,因为您使用单个 View 控制器来显示过滤后的数据。
class TableViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate {
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
filteredArray = country.filter({$0.lowercased().contains(searchText.lowercased())})
if searchText == "" {
filteredArray = country
}
self.tableView.reloadData()
}
var filteredArray = [String]()
var searchController = UISearchController()
var searchBar = UISearchBar()
var resultController = UITableViewController()
let country = ["Argentina", "Australia", "Belgium", "Brazil", "Colombia", "Costa Rica", "Croatia", "Denmark", "Egypt", "England", "France", "Germany", "Iceland", "Iran", "Japan", "Mexico", "Morocco", "Nigeria", "Panama", "Peru", "Poland", "Portugal", "Republic of Korea", "Russia", "Saudi Arabia", "Senegal", "Serbia", "Spain", "Sweden", "Switzerland", "Tunis", "Uruguay"]
@IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
tableView.dataSource = self
tableView.delegate = self
filteredArray = country
searchBar.frame = CGRect(x: 0, y: 0, width: 400, height: 50)
self.tableView.tableHeaderView = searchBar
self.searchBar.delegate = self
definesPresentationContext = true
}
// MARK: - Table view data source
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.filteredArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifier", for: indexPath)
cell.accessoryType = .disclosureIndicator
cell.textLabel?.font = UIFont.systemFont(ofSize: 18.0)
cell.textLabel?.text = self.filteredArray[indexPath.row]
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
performSegue(withIdentifier: "seque", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "seque" {
if let indexPath = self.tableView.indexPathForSelectedRow {
let viewController: ViewController = segue.destination as! ViewController
let newIndex: Int = country.index(of: filteredArray[indexPath.row])!
viewController.myIndex = newIndex
}
}
}
}
请让我知道这是否有效。
编辑
您的课程ViewController
应如下所示:
class ViewController: UIViewController {
@IBOutlet weak var myImageView: UIImageView!
@IBOutlet weak var myImageView2: UIImageView!
@IBOutlet weak var myImageView3: UIImageView!
var myIndex: Int!
override func viewDidLoad() {
super.viewDidLoad()
myImageView.image = UIImage(named: country[myIndex])
myImageView2.image = UIImage(named:country2[myIndex])
myImageView3.image = UIImage(named: country3[myIndex])
}
// Other methods
}
推荐阅读
- python - 新列作为其他列的总和
- python - Altair - 如何使用基准来格式化工具提示
- python - Python - 'And' 运算符在 if 函数中无法正常使用字典
- geometry - 如何衡量一组线段是否比另一组更“曲线”?
- finance - cs50 财务,加载到服务器时找不到 404 错误,注册时出现 500 内部服务器错误
- mysql - 如果发件人 ID 存在于其他表中,而不是只显示收件人 ID
- php - 在php中将十进制值转换为unicode字符
- c# - 从 FTP 下载文件到 Azure 存储的速度很慢 C#
- android - 当 EditText 获得焦点时显示一个风格化的键盘
- c# - 从资源字典中引用整个类