iosswiftuitableviewfooter

How can I make a footer view in the last section of the tableview to NOT STICK at the bottom?


I have a tableview with a footer view that shows up in specific cases in each section. However, if the content of the tableview gets bigger than the view that's actually displayed like in the image, the last footer view in the last section is shown like a preview. I erased out some private informations of our team, the red square is the part that I'm asking about:

Screenshot

Below is the code that I wrote that's related to footerview:

// This is how I registered the footer view in the tableview.
tableView.register(UINib(nibName: "CommentTableViewFooterView", bundle: nil), forHeaderFooterViewReuseIdentifier: "CommentTableViewFooterView")
func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
    let footerView = tableView.dequeueReusableHeaderFooterView(withIdentifier:
                  "CommentTableViewFooterView") as! CommentTableViewFooterView
    // footer에게 CommentViewController 전달
    //footerView.tableView = self.tableView
    footerView.commentVC = self
    footerView.postId = self.postId

    if let cellData = self.commentDataList {
        if cellData[section].recentComment != nil {
            //footerView.backgroundColor = .blue
            footerView.curCommentId = cellData[section].commentSK
            return footerView
        }
        else {
            return nil
        }
    }
    else{
        return nil
    }
}
// remove a strange space..
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
    if let cellData = self.commentDataList {
        if cellData[section].recentComment == nil {
            return .leastNonzeroMagnitude
        }
    }
    return UITableView.automaticDimension
}

I want to make the footer view to be not sticking at the bottom. I googled a lot, but there was nothing helpful to me. I tried making the heightForFooterInSection to return UITableView.automaticDimension just in case, but it didn't work.


Solution

  • It seems like you're using a TableView with Plain style. It will stick the header/footer on top/bottom of itself, according to your content offset. Change to Grouped could fix it.

    tableView = UITableView(frame: view.bounds, style: .grouped)
    

    Or in xib file:

    TableView -> Style -> Grouped