When I search in my searchbar, I want to click on a result and then go to another view controller. I found a similar queston on this site (Swift Search Result Controller in search results segue to another view controller) but it didn't worked out for me. With my current code there is a working segue, but it is the table view segue (instead of the results segue). Does someone know how I do this and explain it in an easy way?
Here's my tableviewcontroller code:
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)
}
}
and here's my second view controller code:
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)
}
}
Here's my customcell controller code:
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
}
}
[First(tableview),second(searching for Neon and clicking on it), third(getting hydrogen("waterstof" in Dutch)1