将数据从TableView发送到DetailView Swift,即使不在UITableList上也是如此

时间:2018-10-26 00:13:35

标签: ios json swift uitableview

每当用户在搜索栏中键入一个名称,并且该名称在我的JSON上并显示在UITableView中时,我都可以单击该名称,并将字符串传递给其他视图控制器。但是现在,我也想保存并传递搜索到的名称,即使它不在我的JSON上也是如此。这样,用户就可以搜索他们想要的任何“医学”名称,并且如果该名称不在我的JSON列表中,它仍会保存并传递给我的其他视图控制器。

这是我的tableView的代码:

import UIKit
import SwiftyJSON

class rxListTableViewController: UITableViewController {
    let searchController = UISearchController(searchResultsController: nil)
    let rxlist = try! JSON(data: NSData(contentsOfFile: Bundle.main.path(forResource: "rxList01", ofType: "json")!)! as Data)
    var filteredRxList = [JSON]()

    override func viewDidLoad() {
        self.tableView.backgroundColor = UIColor.lightGray
        super.viewDidLoad()
        searchController.searchResultsUpdater = self
        searchController.dimsBackgroundDuringPresentation = false
        definesPresentationContext = true
        tableView.tableHeaderView = searchController.searchBar
        tableView.setContentOffset(CGPoint(x: 0, y: searchController.searchBar.frame.size.height), animated: false)
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        navigationItem.hidesSearchBarWhenScrolling = false
    }

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        searchController.isActive = true
        navigationItem.hidesSearchBarWhenScrolling = true
    }

    override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        cell.backgroundColor = UIColor.clear
    }

    // MARK: - Table view data source
    override func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if isFiltering(){
            return filteredRxList.count
        }
        return rxlist.count
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "rxCell", for: indexPath)

        var data: JSON

        if searchController.isActive && searchController.searchBar.text != "" {
            data = filteredRxList[indexPath.row]
            let countryName = data["term"].stringValue

            cell.textLabel?.text = countryName
        }else{
            data = rxlist[indexPath.row]
            tableView.reloadData()
        }

        return cell
    }

    func filteredContentForSearchText(searchText: String){
        filteredRxList = rxlist.array!.filter { country in
            return country["term"].stringValue.localizedLowercase.contains(searchText.localizedLowercase)
        }
        tableView.reloadData()
    }

    func searchBarIsEmpty() -> Bool{
        return searchController.searchBar.text?.isEmpty ?? true
    }

    func isFiltering() -> Bool{
        return searchController.isActive && !searchBarIsEmpty()
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "moreinfo"{
            if let indexPath = tableView.indexPathForSelectedRow{
                let data: JSON
                if isFiltering(){
                    data = filteredRxList[indexPath.row]
                }else{
                    data = rxlist[indexPath.row]
                }
                let controller = segue.destination as? medDetailsViewController
                let final = data["term"].stringValue
                controller?.name = final
                controller?.navigationItem.leftBarButtonItem = splitViewController?.displayModeButtonItem
                controller?.navigationItem.leftItemsSupplementBackButton = true
            }
        }
    } 
}

extension rxListPetsTablViewController: UISearchResultsUpdating{
    func updateSearchResults(for searchController: UISearchController) {
        filteredContentForSearchText(searchText: searchController.searchBar.text!)
    }
}

这是我的viewController代码:

import UIKit

class medDetailsViewController: UIViewController {
    @IBOutlet weak var lbl: UILabel!
    var name = ""

    override func viewDidLoad() {
        super.viewDidLoad()
        lbl.text = "\(name) "
    }

有没有办法实现该请求?

1 个答案:

答案 0 :(得分:0)

在您的下一个视图控制器中,创建一个可选的String变量,并传递值“ searchController.searchBar.text!”。例如,在您的prepare方法中,添加以下代码:

let controller = segue.destination as? medDetailsViewController
controller.searchText = searchController.searchBar.text!