UISearchBar无法跳转到正确的DetailVC

时间:2018-05-27 19:24:43

标签: ios swift uisearchbar

这个想法是当我按下其中一个搜索结果时,他应该指向匹配的DetailVC,但他不这样做,如果我选择任何搜索结果,它只是跳转到总列表中的第一行。 有人可以帮助我,非常感谢

Screenshot,maybe it helps better to explain

import UIKit

class TableViewController: UITableViewController, UISearchResultsUpdating {

    var myData = Indications.readFromBundle()
    var data: [Indications]!

    let searchResultsController = UITableViewController()
    var searchcontroller: UISearchController?
    var resultArray = [Indications]()

    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)

        searchcontroller = UISearchController(searchResultsController: searchResultsController)
        tableView.tableHeaderView = searchcontroller?.searchBar
        searchcontroller?.searchResultsUpdater = self
        searchResultsController.tableView.dataSource = self
        searchResultsController.tableView.delegate = self
    }

    func updateSearchResults(for searchController: UISearchController) {
        if let searchWord = searchcontroller?.searchBar.text{
            resultArray = myData.filter({
                (thisIndication) -> Bool in
                if thisIndication.name.lowercased().contains(searchWord.lowercased()) {
                    return true
                } else {
                    return false
                }
            })
            self.searchResultsController.tableView.reloadData()
        }
    }
    override func prepare(for segue: UIStoryboardSegue,sender: Any?){
        if segue.identifier == "SegueToDetail" {
            let dest = segue.destination as? DetailVC

            let selectedIndexPath = self.tableView.indexPathForSelectedRow
            if let selectedRow = selectedIndexPath?.row {
                dest?.data = myData[selectedRow]
            }
        }

    }

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        print(myData[indexPath.row].name)
        performSegue(withIdentifier: "SegueToDetail", sender: nil)
    }

    override func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if tableView == self.tableView {
            return myData.count
        } else {
            return resultArray.count
        }

    }


    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {


        if tableView == self.tableView {
            let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
            let row = indexPath.row
            cell.textLabel?.text = myData[row].name
            return cell
        } else {
            let cell = UITableViewCell()
            let row = indexPath.row
            cell.textLabel?.text = resultArray[row].name
            return cell
        }


    }

}

import UIKit

class DetailVC: UIViewController {

    var data:Indications!

    @IBOutlet weak var titleNameLabel: UILabel!
    @IBOutlet weak var punktzahlLabel: UILabel!
    @IBOutlet weak var indikationTextLabel: UILabel!
    @IBOutlet weak var myImg: UIImageView!
    @IBOutlet weak var beschreibung: UITextView!
    @IBOutlet weak var indikationTextView: UITextView!

    override func viewDidLoad() {
        super.viewDidLoad()

        addTapGestureToMyImg()

        if data == nil { return }

        titleNameLabel.text = data.name

        punktzahlLabel.text = data.numOfPoint

        indikationTextView.text = data.indication

        beschreibung.text = data.beschreibung

        let name = data.name.replacingOccurrences(
            of: "ü", with: "ue")
        myImg.image = UIImage(named: name)
    }

    func addTapGestureToMyImg() {
        let tapGesture=UITapGestureRecognizer(target: self, action: #selector(handleIntoBigViewImg))
        myImg.addGestureRecognizer(tapGesture)
        myImg.isUserInteractionEnabled=true
    }

    @objc func handleIntoBigViewImg() {
        print("cklik")
        performSegue(withIdentifier: "gotobigimg", sender: data.name)
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "gotobigimg" {
            if let bigImgViewController=segue.destination as? BigImgViewController {
                bigImgViewController.infoFromDetailVC = sender as? String
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您需要为主要内容和过滤内容使用单独的数组。这样,索引将与您在过滤内容上看到的内容相匹配。

伪代码

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "gotobigimg" {

      if let indexPath = tableView.indexPathForSelectedRow {

        let searchBarScopeIsFiltering = searchController.searchBar.selectedScopeButtonIndex != 0
        let isFiltering = searchController.isActive && (!searchBarIsEmpty() || searchBarScopeIsFiltering)

        if isFiltering {
          infoFromDetailVC = filteredArray[indexPath.row]
        } else {
          infoFromDetailVC = array[indexPath.row]
        }

        if let bigImgViewController=segue.destination as? BigImgViewController{
            bigImgViewController.infoFromDetailVC = sender as? String
        }
      }
    }
 } 

Referenced from RayWenderlich