【问题标题】:I get the Error: Fatal error: Index out of range我收到错误:致命错误:索引超出范围
【发布时间】:2020-05-19 22:54:01
【问题描述】:

我正在尝试创建一个连接到我的 tableview 的搜索栏,但是当我运行它时,当单击“返回 searchArray”行上的搜索栏时,我收到一条错误消息,提示“致命错误:索引超出范围” [部分].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()
        }
    }
}

【问题讨论】:

    标签: ios swift xcode uitableview


    【解决方案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()
            }
        }
    }
    

    【讨论】:

    • 你在搜索什么?
    • 索引超出范围意味着您正在访问不在数组中的元素
    • func numberOfSections(in tableView: UITableView) -> Int { return dataArray.count }用搜索和非搜索更新这个方法
    • 搜索部分 1
    • 完全匹配的结果
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-20
    • 2016-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多