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
}
}
}
}
答案 0 :(得分:1)
您的代码存在一些问题:
cellForRow
中,您正在将表视图出列,而不是由委托方法给出的tableView。 self.tableView.dequeueReusableCell(withIdentifier:
此处不使用self
。 resultController
来显示数据。在这里你的应用程序将崩溃prepareForSegue
myIndex
,请在destinationVC中创建一个属性并将其传递给prepareForSegue
。country
数组中选择获取值,这就是您获取错误值的原因。我发现,您不需要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
}