• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

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

[复制链接]
菜鸟教程小白 发表于 2022-12-11 20:13:44 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

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

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 wordStatsecodable{
let word: String
let synonym: String
let meaning: String
let example: String
let video: String

}

这是单词统计



Best Answer-推荐答案


发布崩溃日志。根据我们在这里所拥有的信息,我的猜测是您在 prepareForSegue 中崩溃了:

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

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

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if let destination = segue.destination as? HeroViewController{

            if tableView == resultsController.tableView {
                destination.word = filteredArray[(mainTableView.indexPathForSelectedRow?.row)!]
            } else {
                destination.word = words[(mainTableView.indexPathForSelectedRow?.row)!]
            }
        }
}

关于ios - 如何从表格 View 中显示的搜索结果导航到另一个 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50707783/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap