Swift UITableView didSelectRowAtIndexPath not getting called

前端 未结 10 2008
野的像风
野的像风 2021-02-06 23:18

New to IOS development and am having trouble with handling cell selection on a table. Whenever I select, the method is not getting called below - any idea why?

My proje

相关标签:
10条回答
  • 2021-02-07 00:07

    Everybody is mentioning to set dataSource and delegate of the tableView. But after setting also not working fine then sometimes it may happen because of none or disable selection of table view.

    To enable it Go to storyboard -> Select tableView -> click on the attribute inspector ->go to selector -> Select selection as single selection (or multiple selection according to the requirements.)

    Please find attached screenshot for your suitability.

    0 讨论(0)
  • 2021-02-07 00:07
    • Couple of checks that can help you:-

      myTableView.allowsSelection = true

      myTableView.delegate = self

    • Make sure you written didSelectRowAt correctly:

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

    • If you are using UIButton on UITableViewCell then it overlaps cell so check Solution here

    0 讨论(0)
  • 2021-02-07 00:15

    I faced the same issue when compared two identical code examples where one was working well and the other was not calling didSelectRowAtIndexPath

    Take a look at two possible ways to solve the issue:

    1) In the code itself:

    @IBOutlet weak var table: UITableView!
    
    override func viewDidLoad() {
        table.delegate = self
        table.dataSource = self 
    //data source might be already set if you see contents of the cells
    //the main trick is to set delegate
    }
    

    2) Using Storyboard or Document Outline (which was the problem in my case cause storyboard changes are not visible in .swift controller classes.

    Open Document Outline and Control + Press your TableView you will see two outlets called "delegate" and "dataSource" drag them 1 by 1 to the containing ViewController (right onto the yellow circle)

    That's it!

    0 讨论(0)
  • 2021-02-07 00:15

    Another reason you may write this function which allowed to click under condition

    func tableView(_ tableView: UITableView, shouldHighlightRowAt indexPath: IndexPath) -> Bool {
        if(indexPath.section == 1){
            return true
        }
        return false
    }
    
    0 讨论(0)
提交回复
热议问题