我收到错误:致命错误:索引超出范围

时间:2020-05-19 22:54:01

标签: ios swift xcode uitableview

我试图创建一个连接到我的表视图的搜索栏,但是当我运行它时,单击“ return searchArray”行上的搜索栏时,出现一条错误消息:“致命错误:索引超出范围” [section] .rowTitles.count”。我该如何解决?有什么想法吗?

这是我的代码:

class ViewController: UIViewController {

    @IBOutlet weak var searchBar: UISearchBar!
    @IBOutlet weak var tableView: UITableView!

    struct data {
        var sectionTitle = String()
        var rowTitles = [String]()
    }

    var dataArray = [data(sectionTitle: "section 1", rowTitles: ["row 1", "row 2", "row 3"]),
        data(sectionTitle: "section 2", rowTitles: ["row 1", "row 2"]),
        data(sectionTitle: "section 3", rowTitles: ["row 1"])
       ]
    var searchArray = [(sectionTitle: String, rowTitles: [String])]()
    var searching = false

    override func viewDidLoad() {
        super.viewDidLoad()

        tableView.delegate = self
        tableView.dataSource = self
    }
}

extension ViewController: UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if searching {
            return searchArray[section].rowTitles.count
        } else {
            return dataArray[section].rowTitles.count
        }
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "searchCell")
        if searching {
            cell?.textLabel?.text = self.searchArray[indexPath.section].rowTitles[indexPath.row]
        } else {
            cell?.textLabel?.text = self.dataArray[indexPath.section].rowTitles[indexPath.row]
        }
        return cell!
    }
    func numberOfSections(in tableView: UITableView) -> Int {
        return dataArray.count
    }

    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        if searching {
        return searchArray.count
        } else {
        return dataArray[section].sectionTitle
        }
    }
}


extension ViewController: UISearchBarDelegate {
    func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
        if searchBar.text == nil || searchBar.text == "" {
            searching = false
            view.endEditing(true)
            tableView.reloadData()
        } else {
            searching = true
            searchArray = dataArray.filter({$0.sectionTitle == searchBar.text})
            tableView.reloadData()
        }
    }
}

1 个答案:

答案 0 :(得分:-1)

首先您应该拥有

var searchArray = [data]()

然后

extension ViewController2: UISearchBarDelegate {
    func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
        if searchBar.text == nil || searchBar.text == "" {
            searching = false
            view.endEditing(true)
            tableView.reloadData()
        } else {
            searching = true
           searchArray = dataArray.filter({$0.sectionTitle.lowercased().contains(searchBar.text!.lowercased()) || $0.rowTitles.map{$0.lowercased()}.contains(searchBar.text!.lowercased()) })
            tableView.reloadData()
        }
    }
}