0

I have footer func. And i want to show separator at top of this cell and hide it at bottom.

override func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {

        let footer = UITableViewCell(frame: CGRect(x: 0, y: 0, width: tableView.frame.width, height: 50))


        let label = UILabel(frame: CGRect(x: 0, y: 0, width: tableView.frame.width, height: 50))

        label.text = "HELLO"
        label.textAlignment = .center
        label.textColor = UIColor.lightGray



        footer.addSubview(label)

        tableView.tableFooterView = footer

        return footer

    }

How could I turn on separator there programmatically?

Ivan Kisin
  • 385
  • 1
  • 3
  • 12

1 Answers1

0

There are a few things you should change in your code:

  • There is no point in wrapping your footer view in a UITableViewCell, you can instead directly use the UILabel.
  • The tableView(_:viewForFooterInSection:) method you are overriding is meant to provide a footer for each section in the table; however, you also set tableFooterView property of the table view, which is used to set the single "global" footer of the table (a few at the very bottom of the table, regardless of how many sections it contains). So the same view will be used in two places, which is not allowed. Choose one.

When you have fixed these two issues, you can add the separator to your footer label as described here. Note that nowadays, the preferred way of laying out views is not to manually set the position and size but to use AutoLayout constraints (for which I suggest you read a good tutorial), but for a start the linked solution should work for your.

dr_barto
  • 5,723
  • 3
  • 26
  • 47