Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 my UITableView footer did not show up. It turns out that none of my UITableViewDelegate methods get called (ex: func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView?).

Interestingly, the UITableViewDataSource methods are being called and the table is being populated. I have set the parent view controller to be the table's delegate and dataSource.

To illustrate the problem, I have created a sample Swift 3.0 project tailored to match my existing codebase as much as possible. Maybe something changed in Swift 3/Xcode 8 that I am not aware of or I might be missing something really obvious. Thank you for your help!

like image 818
Plague Avatar asked Dec 24 '22 00:12

Plague


2 Answers

After I checked your sample project:

You didn't did anything wrong, but referring to viewForFooterInSection, it will not get called until you implement heightForFooterInSection method:

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

Similar Cases:

No implementing for heightForHeaderInSection ==> No calling for viewForHeaderInSection even if it's implemented.

Returning numberOfRowsInSection as zero ==> No calling for cellForRowAt even if it's implemented.

Additional Note: you don't have to add tableView.dataSource = self and tableView.delegate = self in viewDidLoad(), they have been set in the interface Builder.

like image 117
Ahmad F Avatar answered May 20 '23 02:05

Ahmad F


You should set the height of the footer

 func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
    return 15
}
like image 38
Rob Avatar answered May 20 '23 04:05

Rob