2017-09-22 12 views
0

我是新来的代码,我只是无法找到解决这个问题。互联网上的大多数答案都被弃用或未答复。斯威夫特 - 顺着接下去错误索引时的UISearchBar活跃

我想寻找的东西进入我的搜索栏,在那之后我想按的结果之一。当我点击结果时,我想让它在另一个视图控制器中给我相应的信息。

这是我的问题:当我点击一个结果时,其他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) 

    } 
} 

这里是我的代码从视图 - 控制:

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

} 

回答

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] 
     } } 
} 
+0

Thx for comment! destination.name是什么意思? @LuizPerez –

+0

DestinationVC是你的下一个视图控制器,您选择的tableview的一行后会呈现出一个,即视图 - 控制需要,可容纳要传递的值的变量,所以destinationVC.name是引用变量。 –

+0

当我输入destinationVC.name时,他给出了一个错误。 scheikundeformules“的类型值‘’没有成员‘名’@LuisPerez –

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 = 0scheikundeformule控制器。如下所示:

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