Swift - 在UISearchBar处于活动状态时查看错误的索引

时间:2017-09-22 18:48:29

标签: ios swift search tableview

我是新手代码,我找不到解决这个问题的方法。互联网上的大多数答案都已弃用或未答复。

我想在搜索栏中搜索一些内容,之后我想按其中一个结果。当我点击结果时,我希望它在另一个viewcontroller中给我相应的信息。

这是我的问题:当我点击结果时,另一个viewcontroller中的信息与结果给我的信息不一致。他所做的是给出了与tableview相对应的信息。所以,它用错误的索引来表达。有谁知道解决方案是什么?

这是我的代码(带有搜索栏的tableviewcontroller):

   var myIndex = 0
   extension UIColor { //themakleur
   static let candyGreen = UIColor(red: 71.0/255.0, green: 81.0/255.0, blue: 
   89.0/255.0, alpha: 1.0)}

   var watjeberekend = ["Omtrek Cirkel","Oppervlakte Cirkel","Lengte 
   Boog","Oppervlakte Sector","Oppervlakte Bol"]

class scheikunde: UITableViewController, UISearchResultsUpdating {

var scheikundeformules = ["Omtrek Cirkel","Oppervlakte Cirkel","Lengte Boog","Oppervlakte Sector","Oppervlakte Bol"]
var searchcontroller : UISearchController!
var filterednamen = [String]()


override func viewDidLoad() {
    super.viewDidLoad()

    self.searchcontroller = UISearchController(searchResultsController: nil)
    self.tableView.tableHeaderView = self.searchcontroller.searchBar
    self.searchcontroller.searchResultsUpdater = self
    self.searchcontroller.dimsBackgroundDuringPresentation = false
    definesPresentationContext = true
    self.navigationItem.hidesBackButton = false;
    self.navigationController?.isNavigationBarHidden = false

    //navigationcontroller layout
    navigationController?.navigationBar.barTintColor = UIColor(red: 71.0/255.0, green: 81.0/255.0, blue: 89.0/255.0, alpha: 1.0)
    navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white]
    navigationController?.navigationBar.layer.borderColor = UIColor.candyGreen.cgColor
    navigationController?.navigationBar.layer.borderWidth = 0

    //searchcontroller layout
    searchcontroller.searchBar.layer.borderWidth = 0
    searchcontroller.searchBar.layer.borderColor = UIColor.candyGreen.cgColor
    UISearchBar.appearance().barTintColor = .candyGreen
    UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).tintColor = .candyGreen
    searchcontroller.searchBar.backgroundImage = UIImage(named: "themakleur.jpg")
    let cancelButtonAttributes: [String: AnyObject] = [NSForegroundColorAttributeName: UIColor.white]

    UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).setTitleTextAttributes(cancelButtonAttributes, for: .normal)

}

func updateSearchResults(for searchController: UISearchController) {
    self.filterednamen = self.scheikundeformules.filter { (naam : String) -> Bool in
        if naam.lowercased().contains(self.searchcontroller.searchBar.text!.lowercased()) {
            return true
        }else{
            return false}}
        self.tableView.reloadData()
    }


override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if !searchcontroller.isActive || searchcontroller.searchBar.text == "" {
        return self.scheikundeformules.count
    }else {
        return self.filterednamen.count
        }
    }


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

    cell.label.text = scheikundeformules[indexPath.row]

    if !searchcontroller.isActive || searchcontroller.searchBar.text == "" {
        //cell.textLabel?.text = self.namen[indexPath.row]
        cell.label.text = self.scheikundeformules[indexPath.row]
    }else{
        //cell.textLabel?.text = self.filterednamen[indexPath.row]
        cell.label.text = self.filterednamen[indexPath.row]
        }
        return cell
    }



override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    myIndex = indexPath.row
    performSegue(withIdentifier: "segue", sender: self)

    }
}

以下是来自viewcontroller的代码:

        import UIKit


        class scheikundeformule: UIViewController{

@IBOutlet var uitleg6: UILabel!
@IBOutlet var formuleomschrijving: UILabel!
@IBOutlet var uitleg5: UILabel!
@IBOutlet var uitleg4: UILabel!
@IBOutlet var uitleg3: UILabel!
@IBOutlet var uitleg2: UILabel!
@IBOutlet var uitleg1: UILabel!
@IBOutlet var titlelabel: UILabel!

override func viewDidLoad() {
    super.viewDidLoad()
    titlelabel.text = scheikundeformules[myIndex]
    uitleg1.text = uitlegformules2[myIndex]
    uitleg2.text = uitlegformules3[myIndex]
    uitleg3.text = uitlegformules4[myIndex]
    uitleg4.text = uitlegformules5[myIndex]
    uitleg5.text = uitlegformules6[myIndex]
    uitleg6.text = uitlegformules7[myIndex]

    self.navigationItem.hidesBackButton = false;
    self.navigationController?.isNavigationBarHidden = false
    formuleomschrijving.text = watjeberekend[myIndex]

    //keyboard weg deel 1
    let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(startscreen.dismissKeyboard))

    //Uncomment the line below if you want the tap not not interfere and cancel other interactions.
    //tap.cancelsTouchesInView = false
    view.addGestureRecognizer(tap)

}
override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()

 }

2 个答案:

答案 0 :(得分:0)

你的职能在哪里为segue做准备?如果你想将数据传递给下一个视图控制器,你需要在有趣的准备segue中发送它,并记住indexpath.row需要与作为tableview的当前数据源的数组一起使用,解决方案将是这样的:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
 if let destinationVC = segue.destination as? YourViewController {
     if !searchcontroller.isActive {
         destinationVC.name = filterednamen[myIndex]
     } else {
            destinationVC.name = scheikundeformules[myIndex]
       }  }
}

答案 1 :(得分:0)

在故事板中添加scheikundeformule视图控制器的标识符,如下所示:

第1步:

Step 1:

第2步: 改变你的tableview didSelectRow如下:

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    myIndex = indexPath.row

    let scheikundeformuleController = self.storyboard?.instantiateViewController(withIdentifier: "scheikundeformule") as! scheikundeformule
    scheikundeformuleController.myIndex = indexPath.row
    self.present(scheikundeformuleController, animated: true, completion: nil)
}

第3步:

您必须将var myIndex = 0添加到scheikundeformule控制器。如下所示:

class scheikundeformule: UIViewController{
    var myIndex = 0
    ........Your code
}