UITableViewDelegate methods not called after Swift 3.0 and Xcode 8 migration

After migrating my codebase from Swift 2.2 to Swift 3.0, I noticed that the UITableView footer did not appear. It turns out that none of my UITableViewDelegate methods are called (ex: func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? ).

Interestingly, the UITableViewDataSource methods are called and the table is populated. . I installed the parent view controller in the delegate table and dataSource .

To illustrate the problem, I created a Swift 3.0 project sample to best fit my existing codebase. Maybe something has changed in Swift 3 / Xcode 8, which I don’t know about, or maybe I missed something really obvious. Thanks for the help!

+3
source share
3 answers

After I checked your sample project:

You haven’t done anything wrong, but referring to viewForFooterInSection , it will not be called until you implement the heightForFooterInSection method:

 func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat { return 50 // for example } 

Similar cases:

There is no implementation for heightForHeaderInSection ==> There is no call for viewForHeaderInSection , even if it is implemented.

Return numberOfRowsInSection as zero ==> There is no call to cellForRowAt , even if it is implemented.

Additional note: you do not need to add tableView.dataSource = self and tableView.delegate = self to viewDidLoad() , they were set in the Builder interface.

+5
source

You have to set the footer height

  func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat { return 15 } 
+3
source

In Xcode8 or the latest version of Swift, the method signature has been changed. This change may cause your code to not invoke the delegation method.

Fix this by typing the method again using autocomplete.

The old method is

 func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat? { return 50.0 } 

The new method is

 func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { return 50.0 } 

heightForHeaderInSection should be used when using viewForHeaderInSection .

Hope this helps.

+1
source

Source: https://habr.com/ru/post/1264782/


All Articles