首页 > 解决方案 > 过滤(搜索表)后,项目选择不起作用(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
            }
        }
    }
}

标签: iosswift

解决方案


您的代码中存在一些问题:

  1. cellForRow您使您的表视图出队时,而不是委托方法给出的表视图。self.tableView.dequeueReusableCell(withIdentifier:这里不要使用self.
  2. 您正在使用resultController没有任何原型单元格来显示数据。在这里,您的应用程序将崩溃。
  3. 我不明白你做了什么prepareForSegue
  4. 不要使用任何全局变量。如果要使用myIndex,请在destinationVC 中创建一个属性并传递它prepareForSegue
  5. 您在表格视图和选择从数组中获取值时显示过滤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
}

推荐阅读