UITableViewDelegate methods not called after Swift 3.0 and Xcode 8 migration

前端 未结 3 1036
予麋鹿
予麋鹿 2021-01-19 09:10

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 UITableViewDe

相关标签:
3条回答
  • 2021-01-19 09:39

    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.

    0 讨论(0)
  • 2021-01-19 09:46

    In Xcode8, or latest Swift version, the method signature has been changed. This change might be causing your code to not call delegate method.

    Fix it by writing method again using autocomplete.

    Old Method -

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

    New Method -

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

    heightForHeaderInSection has to be used while using viewForHeaderInSection.

    Hope this helped.

    0 讨论(0)
  • 2021-01-19 09:47

    You should set the height of the footer

     func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 15
    }
    
    0 讨论(0)
提交回复
热议问题