Can't get didSelectRowAt to work for TableView

☆樱花仙子☆ 提交于 2019-12-24 01:12:41

问题


I am having trouble getting the didSelectRowAt method to work for a TableView inside of a regular ViewController. I have already made sure that the delegate and data source for the table are set in the ViewController code. This ViewController populates the tableview cells with results from a search query to an API, and the rendering of cell data is working fine.

It's just the didSelectRowAt method that is not registering. I did try manually adding the same delegate information on the Main.storyboard, but the little + sign won't trigger any popup windows. I am wondering if there is something in the Main.storyboard that needs fixing. I have attached the images of the ViewController and TableView connections inspector as well. I am new to iOS development and don't have much experience with graphic interfaces for mobile design, so I am assuming it's something there but maybe I am wrong.

Here's the basic version of my code:

class SearchViewController: UIViewController, UISearchBarDelegate, UITableViewDelegate, UITableViewDataSource {


@IBOutlet weak var tableView: UITableView!
@IBOutlet var searchBar: UISearchBar!

   ...variable declarations ....

override func viewDidLoad() {
    super.viewDidLoad()
    self.hideKeyboardWhenTappedAround()
    searchResults = []
    searchBar.delegate = self
    tableView.dataSource = self
    tableView.delegate = self

}

func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1;
}

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

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

    let cell = tableView.dequeueReusableCell(withIdentifier: "searchTableViewCell", for: indexPath) as! SearchTableViewCell

    if(searchActive && !(self.searchResults?.isEmpty)!) {
        (doing some stuff with search results here...works fine)
    }
    return cell
}

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

func searchBar(_ searchBar: UISearchBar,
               textDidChange searchText: String) {
    print("search text \(searchText)")
    getSearchResultJSON(term: searchText) { json in
        let data = json as! [Dictionary<String, String>]
        self.searchResults = data
    }
    self.tableView.reloadData()
    }
...
}

[

]

[

]

EDIT: as a sanity check for if the search asynchronous function was changing anything, I just tried removing all search-related code and filling the tableview from a hardcoded dummy variable array. It worked to display the dummy variables, but still no ability to select a cell and get any reaction. I also saw a couple mentions that I had previously had a typo with didDeSelectRowAt instead of didSelectRow at, that has been fixed but the behaviour is the same.

This ended up being related to a tap gesture that occurs in the hideKeyboardWhenTappedAround() extension that I wrote


回答1:


Found it! The culprit was the self.hideKeyboardWhenTappedAround(), which is an extension I wrote to hide the keyboard. This interfered with the tap of a cell because it did indeed utilize UITapGestureRecognizer. Thanks for the hints everyone.




回答2:


You are using didDeselectRowAt instead of didSelectRowAt

Edit

Well, use this below delegate then

func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
    return true
}

and make your controller conform to UIGestureRecognizerDelegate




回答3:


If you are using tap gesture on main view then table view cell did select method is not working properly.




回答4:


In picture you uploaded, delegate and datasource aren't connected to the ViewController. Remove the code in viewdidload (tableview.delegate = self) and connect them in storyboard.



来源:https://stackoverflow.com/questions/41499421/cant-get-didselectrowat-to-work-for-tableview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!