Swift搜索结果segue到其他View控制器

时间:2017-09-02 08:51:53

标签: ios swift tableview uisearchresultscontroller

当我在搜索栏中搜索时,我想点击一个结果,然后转到另一个视图控制器。我在这个网站上发现了一个类似的问题(Swift Search Result Controller in search results segue to another view controller),但这对我没有用。 使用我当前的代码有一个工作segue,但它是表视图segue(而不是结果segue)。 有人知道我是怎么做的并以一种简单的方式解释它吗?

这是我的tableviewcontroller代码:

class elementstableviewcontroller: UITableViewController,
UISearchResultsUpdating{


var namen = ["Waterstof","Helium","Litium"]

var searchcontroller: UISearchController!
var resultsController: 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

}
func updateSearchResults(for searchController: UISearchController) {
    self.filterednamen = self.namen.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.namen.count
    }else {
        return self.filterednamen.count
    }
}

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

    cell.name.text = namen[indexPath.row]


    if !searchcontroller.isActive || searchcontroller.searchBar.text == "" {

        cell.name.text = self.namen[indexPath.row]


    }else{

        cell.name.text = self.filterednamen[indexPath.row]

    }
    return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    myIndex2 = indexPath.row
    performSegue(withIdentifier: "segue2", sender: self)

    }



 }

这是我的第二个视图控制器代码:

  import UIKit
  import GoogleMobileAds


 class Elementenuitgelegd: UIViewController, GADBannerViewDelegate{

@IBOutlet var banner: GADBannerView!
@IBOutlet var uitleg5: UILabel!
@IBOutlet var groep: UILabel!
@IBOutlet var periode: UILabel!
@IBOutlet var uitleg4: UILabel!
@IBOutlet var uitleg3: UILabel!
@IBOutlet var uitleg2: UILabel!
@IBOutlet var uitleg1: UILabel!
@IBOutlet var titlelabel: UILabel!
var distance: Double = 0;
var speed: Double = 0;
var time: Double = 0;

override func viewDidLoad() {
    super.viewDidLoad()
    titlelabel.text = namen[myIndex2]
    uitleg1.text = afkortingen[myIndex2]
    uitleg2.text = atoommassas[myIndex2]
    uitleg3.text = atoomnummers[myIndex2]
    uitleg4.text = electronenconfig[myIndex2]
    periode.text = periodes[myIndex2]
    groep.text = groepen[myIndex2]

    self.navigationItem.hidesBackButton = true;
    self.navigationController?.isNavigationBarHidden = true

    //banner
    let request = GADRequest()
    request.testDevices = [kGADSimulatorID]
    banner.adUnitID = "ca-app-pub-8478021432040036/4295805958"
    banner.rootViewController = self
    banner.delegate = self
    banner.load(request)


    //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()
    // Dispose of any resources that can be recreated.
}

   //keyboardweg deel 2
func dismissKeyboard() {
    view.endEditing(true)
}

}

这是我的customcell控制器代码:

 import UIKit

class CustomCell: UITableViewCell {

   @IBOutlet var name: UILabel!

   override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code
}

override func setSelected(_ selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)

    // Configure the view for the selected state
}

}

[第一次(桌面视图),第二次(搜索霓虹灯并点击它),第三次(获得氢气(荷兰语中的“waterstof”)1

0 个答案:

没有答案