为什么UITableView页脚出现在最后一个单元格的顶部?

时间:2015-06-06 10:51:13

标签: ios objective-c uitableview

我有一个UITableView,其中包含一堆单元格(动态生成),并显示一个显示上次更新时间的页脚消息。我遇到的问题是,在第一次加载UITableView时,似乎在最后一个单元格的顶部显示了页脚。重新加载数据后,它会正确显示。

有人知道为什么会这样吗?

初始加载:

enter image description here

重新加载后:

enter image description here

以下是我用于建立要显示的页脚消息的逻辑:

override func tableView(tableView: UITableView, titleForFooterInSection section: Int) -> String? {

    var title = ""
    if (section == kSectionA) {
        if (!self.hasInternet) {
            title = "No Internet Connection"
        } else {
            if (self.dataToDisplay) {
                title = "Last Updated: xx:xx AM"
            } else {
                title = "No results found."
            }
        }
    }

    return title
}

1 个答案:

答案 0 :(得分:1)

您需要告诉Tableview从一开始就有多大的页脚。尝试使用此代码,但使用您的值。

告诉Tableview页脚有多大:

override func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
let footerView = UIView(frame: CGRectMake(0, 0, tableView.frame.size.width, 40))
footerView.backgroundColor = UIColor.grayColor()

return footerView
}

告诉页脚页脚有多大:

override func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 40.0
}

那应该可以解决你的问题。