ios - Swift - 当 UISearchBar 处于事件状态时转到错误的索引

标签 ios swift search tableview

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

我想在我的搜索栏中搜索一些东西,然后我想按下其中一个结果。当我点击结果时,我希望它在另一个 View Controller 中给我相应的信息。

这是我的问题:当我点击一个结果时,另一个 View Controller 中的信息与结果给我的信息不一致。他给我的是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)

    }
}

这是我在 View Controller 中的代码:

        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()

 }

最佳答案

你的函数在哪里为 segue 做准备?如果你想将数据传递给下一个 View Controller ,你需要在有趣的准备中发送它,并记住 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]
       }  }
}

关于ios - Swift - 当 UISearchBar 处于事件状态时转到错误的索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46371393/

相关文章:

javascript - 移动设备上的电话号码链接检测

java - java中的Typeahead/增量搜索

java - 在 Java 中的单词之间查找文本

Swift:将数据传递给 NavigationController 的第一个子节点,使用 storyboardID 实例化

string - MS SQL - 如何获取从大字符串中搜索的逗号/分号分隔的电子邮件地址列表

objective-c - 如何在 iOS 应用程序中实现信号量?

ios - 如何获取带有换行符(\n)的 UITextView 的内容高度?

ios - Swift 比较日期与字符串日期值

ios - TabBarController 与 NavigationController

swift - 核心数据关系 : Filter and Sort