-1

Hi guys I have been trying for few days no answer found . I have already implemented UITableViewDelegate and UITableViewDataSource before raising this question my didSelectRowAt and didDeselectRowAt, both the methods are not working. enter image description here

class SearchClass: UIViewController, UITableViewDataSource,UITableViewDelegate, UISearchBarDelegate {

@IBOutlet weak var myTableView: UITableView!
@IBOutlet weak var mySearchBar: UISearchBar!

var objects:PFObject!

var searchResults = [String]()

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    self.navigationController?.navigationBar.topItem?.title = "Search"
    self.navigationController?.navigationBar.barTintColor = UIColor.white
    self.navigationController?.navigationBar.backgroundColor = UIColor.black
    self.navigationController?.navigationBar.tintColor = UIColor.black
    self.searchResults.removeAll()

}


override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    self.myTableView.dataSource = self
    self.mySearchBar.delegate = self
    self.myTableView.delegate = self


    self.navigationController?.extendedLayoutIncludesOpaqueBars = true

    //self.myTableView.reloadData()
}

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

func searchBarSearchButtonClicked(_ searchBar: UISearchBar)
{
    searchBar.resignFirstResponder()
   // self.navigationController?.navigationBar.isHidden = false
    self.mySearchBar.endEditing(true)
    print("Search word = \(searchBar.text)")

    let query = PFQuery(className:"myClass")
    //let newText = searchBar.autocapitalization
    searchBar.autocapitalizationType = .none
    searchBar.text = searchBar.text?.localizedLowercase
    query.whereKey("collegeNickName", contains: searchBar.text)

    query.findObjectsInBackground { (results, error) in
        if error == nil {

            if let objects = results {

                self.searchResults.removeAll(keepingCapacity: true)

                for object in objects {
                    let firstName = object.object(forKey: "myName") as! String
                    let image = object.object(forKey: "myImage") as! PFFile
                    //   let lastName = object.object(forKey: "myPlace") as! String
                    //   let fullName = firstName + " " + lastName
                    self.searchResults.append(firstName)

                    print(self.searchResults[0])
                    DispatchQueue.main.async {
                        self.myTableView.reloadData()
                        self.mySearchBar.resignFirstResponder()
                    }
                }
            }

        } else {

            let myAlert = UIAlertController(title:"Alert", message:error?.localizedDescription, preferredStyle:UIAlertControllerStyle.alert)

            let okAction = UIAlertAction(title: "Ok", style: UIAlertActionStyle.default, handler: nil)

            myAlert.addAction(okAction)

            self.present(myAlert, animated: true, completion: nil)
            return


                }
            }
        }

func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
    //self.navigationController?.navigationBar.isHidden = true
}


func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
    return searchResults.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
    myTableView.register(UITableViewCell.self, forCellReuseIdentifier: "myCell")

    let myCell = myTableView.dequeueReusableCell(withIdentifier: "myCell")

    myCell?.textLabel?.text = searchResults[indexPath.row]
    print(searchResults[indexPath.row])
    return myCell!
}


func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    print("Hi")
}

func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {

}

func searchBarCancelButtonClicked(_ searchBar: UISearchBar)
{
    mySearchBar.resignFirstResponder()
    mySearchBar.text = ""
    myTableView.reloadData()
}

func searchBarTextDidEndEditing(_ searchBar: UISearchBar) {
    //self.mySearchBar.resignFirstResponder()
    //self.mySearchBar.endEditing(true)
    self.definesPresentationContext = true
}

@IBAction func refreshButtonTapped(sender: AnyObject) {
    mySearchBar.resignFirstResponder()
    mySearchBar.text = ""
    self.searchResults.removeAll(keepingCapacity: false)
    self.myTableView.reloadData()
  }
}

Also it implements a searchView I'm getting what I want to search but unable use select and deselect methods in my class.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Sabhay Sardana
  • 876
  • 1
  • 10
  • 27

4 Answers4

1
self.myTableView.dataSource = self
self.myTableView.delegate = self

<--- add this.

  • add it to your code. you have only .datasource = self there as far as I can see –  Jan 21 '17 at 18:46
  • I was seeing `mySearchView.delegate` as `myTableView.delegate` working like a charm. Thanks for pointing out my mistake – Sabhay Sardana Jan 21 '17 at 18:54
0

Noticed that you've set delegate in the storyboard as view instead of your UIViewController see Discover - that's mine UViewController

0

In Interface Builder

  • Connect dataSource and delegate of My Table View to SearchClass

Then you can delete redundant self.myTableView.dataSource = self in SearchClass

Consider that connections in Interface Builder are more efficient than in code.

vadian
  • 274,689
  • 30
  • 353
  • 361
0

Declaration of cell is wrong. you have done this below code

 let myCell = myTableView.dequeueReusableCell(withIdentifier: "myCell")

Right format

 let myCell = tableView.dequeueReusableCell(withIdentifier: "myCell")

Reason: When you use dequeue property, then UITableView dequeue its cell by the param it got from the method

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell

see there is a param tableView but you are dequeing by the outllet of UITableView.

dahiya_boy
  • 9,298
  • 1
  • 30
  • 51