Swift UITableView didSelectRowAtIndexPath not getting called

前端 未结 10 2010
野的像风
野的像风 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: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!

提交回复
热议问题