如何从表格视图中显示的搜索结果导航到另一个视图?

时间:2018-06-05 19:41:42

标签: ios swift searchbar

以下代码是我的viewController代码。 json文件中的单词显示在tableView中,单击一个单词,将其带到另一个页面,其中显示与该单词关联的元素。但是,当在表视图中显示搜索结果时,我无法导航到该页面。

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UISearchResultsUpdating{

@IBOutlet weak var mainTableView: UITableView!

var words = [wordStats]()
var filteredArray = [wordStats]()
var searchController = UISearchController()
var resultsController = UITableViewController()


override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    getJson {
        self.mainTableView.reloadData()
    }

    mainTableView.delegate = self
    mainTableView.dataSource = self

    searchController = UISearchController(searchResultsController: resultsController)
    mainTableView.tableHeaderView = searchController.searchBar
    searchController.searchResultsUpdater = self

    resultsController.tableView.delegate = self
    resultsController.tableView.dataSource = self

}

func updateSearchResults(for searchController: UISearchController) {
    filteredArray = words.filter({ (words: wordStats) -> Bool in
        if words.word.contains(searchController.searchBar.text!){
            return true
        }else{
            return false
        }
    })
    resultsController.tableView.reloadData()
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if tableView == resultsController.tableView{
        return filteredArray.count
    }else{
        return words.count
    }
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = UITableViewCell(style: .default, reuseIdentifier: nil)
    if tableView == resultsController.tableView{
        cell.textLabel?.text = filteredArray[indexPath.row].word.capitalized
    }else{
        cell.textLabel?.text = words[indexPath.row].word.capitalized
    }
    //cell.textLabel?.text = words[indexPath.row].word.capitalized
    return cell
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    performSegue(withIdentifier: "wordDetails", sender: self)
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if let destination = segue.destination as? HeroViewController{
            destination.word = words[(mainTableView.indexPathForSelectedRow?.row)!]
        }
}

func getJson(completed: @escaping () -> ()){
    let path = Bundle.main.path(forResource: "week_14", ofType: "json")
    let url = URL(fileURLWithPath: path!)
    let data = try! Data(contentsOf: url)
            do {
                self.words = try JSONDecoder().decode([wordStats].self, from: data)
                DispatchQueue.main.async{
                    completed()
                }
            }catch{
                print("JSON error")
            }
}

}

最初从JSON文件获取数据后,数据显示在mainTableView中,但在搜索后我点击项目应用程序崩溃。有人可以帮帮我吗?

struct wordStats:Decodable{
let word: String
let synonym: String
let meaning: String
let example: String
let video: String

}

这是wordStats

2 个答案:

答案 0 :(得分:0)

发布崩溃日志。根据我们在这里所拥有的一点点,我的猜测是你在pipeline { agent any stages { stage('test'){ steps { script { def props = readProperties text:"hello1=world1\nhello2=world2" for (prop in props) { String name = prop.key String value = prop.value println "prop $name is $value" //httpRequest httpMode: 'POST', url: "http://myUrl", requestBody: "Name=$name&Value=$value", contentType: 'APPLICATION_FORM' } } } } } } 崩溃:

prepareForSegue

使用过滤后的结果可能是索引错误。这可能会解决它:

destination.word = words[(mainTableView.indexPathForSelectedRow?.row)!]

答案 1 :(得分:0)

这就是最终的作用

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if let destination = segue.destination as? HeroViewController {
        if let indexPath = mainTableView.indexPathForSelectedRow{
            if isFiltering(){
                destination.word = filteredArray[indexPath.row]
            }else{
                destination.word = words[indexPath.row]
            }
        }
    }
}