Search code examples
iosswiftuitableviewuisearchbaruipickerview

UIPickerView and UISearchController not displaying data


I am experiencing some trouble at the moment and was hoping one of the experts could assess and hopefully answer my question. I have made a UIPickerView and a UISearchController (which is in a UITableViewController) and both of which have no been showing the data from the arrays I made. This is a school project and is not due till Tuesday, but I am hoping I can fix these small (hopefully bugs). I will show you my ViewControllers for both the UIPickerView and the UISearchController. So here is the UIPickerView :


import UIKit

class SecondViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource, UITextFieldDelegate {

@IBOutlet weak var txtTest: UITextField!

@IBOutlet weak var Picker1: ThePickerView!
var sellInvestments = ["Airbnb", "CloudFlare", "GitHub", "slack", "Snapchat", "Uber"]

override func viewDidLoad() {
    super.viewDidLoad()

// Do any additional setup after loading the view.

    self.Picker1.delegate = self
    self.Picker1.dataSource = self
    [self.Picker1.reloadAllComponents]
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}
func pickerView(_pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
    return sellInvestments[row]
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    return sellInvestments.count
}
public func numberOfComponents(in pickerView: UIPickerView) -> Int {

    return 1
}

}

And now here is the UITableViewController (for the UISearch)

import UIKit

class NewTableViewController: UITableViewController, UISearchResultsUpdating {

let searchInvestments = ["Berlark Company", "Snapchat", "IBM", "Twitter", "Cornerstone on Demand", "Aukvern Capital", "Serene Technologies", "Viacom Industries", "Suvertec", "HIppit", "Avigti Technologies", "Avaret", "Sivlot", "Zebra Sci Automation", "Google", "Apple", "Facebook", "Gradience Imaging", "Vitris", "Voxtrat", "WhattsApp", "Apphat", "Nividia", "Kik", "Cyber Dust", "Turing Technologies", "Sobel Imaging Systems", "Yavid", "Tensor Automation", "Vistapint", "LinkedIn", "Yahoo", "Yelp", "TwitchTv", "OculusRift", "Lg", "Intel", "Amazon", "Sony", "Samsung", "Microsoft", "HP", "Vencore", "AT&T", "Verizon", "Dell", "MicroTech", "Flickr"]
var filteredInvestments = [String]()
var resultSearchController = UISearchController()
override func viewDidLoad() {
    super.viewDidLoad()
    self.resultSearchController = UISearchController(searchResultsController: nil)
    self.resultSearchController.searchResultsUpdater = self
    self.resultSearchController.dimsBackgroundDuringPresentation = false
    self.resultSearchController.searchBar.sizeToFit()
    self.tableView.tableHeaderView = self.resultSearchController.searchBar
    self.tableView.reloadData()



    // Uncomment the following line to preserve selection between presentations
    // self.clearsSelectionOnViewWillAppear = false

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem()
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

// MARK: - Table view data source

override func numberOfSections(in tableView: UITableView) -> Int {
    // #warning Incomplete implementation, return the number of sections
    return 1
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete implementation, return the number of rows
    if self.resultSearchController.isActive
    {
       return self.filteredInvestments.count
    }
    else {
        return self.searchInvestments.count

    }

}


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

    if self.resultSearchController.isActive
    {
        cell!.textLabel?.text = self.filteredInvestments[indexPath.row]
    }
    else {
        cell!.textLabel?.text = self.searchInvestments[indexPath.row]
    }

    return cell!

}
func updateSearchResults(for searchController: UISearchController) {
    self.filteredInvestments.removeAll(keepingCapacity: false)
   let searchPredicate = NSPredicate(format: "SELF CONTAINS[c] %@", searchController.searchBar.text!)
  let array  = (self.filteredInvestments as NSArray).filtered(using: searchPredicate)
    self.filteredInvestments = array as! [String]
    self.tableView.reloadData()



}

}

OK so there is the code of both the PickerView and Search view. *There are no compiling errors and the app does run. I have it hooked up to my iPhone 5c and am able to run the app nicely.

Here are some pictures of the app running. (With the unfilled views)

here are the links to the images (because I don't have a 10 reputation) https://i.sstatic.net/whtl0.jpg - PickerView https://i.sstatic.net/kfK2a.jpg - UISearch

Thank you so much if you are able to answer this, because I have looked around the web and most of the time there was little to no help, and the ones with help, turned out not to do anything. So to summarize, the strings I put into my arrays are not showing up on either when I run it.

Thank you Very Much guys!


Solution

  • For your UIPickerView you forgot the function:

     func pickerView(_ pickerView: UIPickerView, viewForRow row: Int,     forComponent component: Int, reusing view: UIView?) -> UIView 
    {
     let pickerLabel = UILabel() //Create label
     pickerLabel.textColor = UIColor.black //Create text color
     pickerLabel.text = pickerData[row] //Make text for row the one in array
     pickerLabel.font = UIFont(name: "Helvetica-Light", size: 20) //Text font
     pickerLabel.textAlignment = NSTextAlignment.center //Alignment
     return pickerLabel
    }
    

    As for the search, use a similar function as above, just without the picker view it will be for search view. Autocomplete should help find it.