I have a table with a header and with several sections (each with a header) and I would need both the table header and the section headers to be fixed... is that possible using a UITableView ?
Here is the code for my table:
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 40
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
switch (section)
{
case 0:
return 0
case 1:
return self.heigthHeader(results: self.transactions_PND)
case 2:
return self.heigthHeader(results: self.transactions_PAY)
case 3:
return self.heigthHeader(results: self.transactions_RJC)
case 4:
return self.heigthHeader(results: self.transactions_DUE)
default:
return 0
}
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerView: UIView = UIView(frame: CGRect(x: 20, y: 0, width: 100, height: 60))
let label : UILabel = UILabel()
label.text = sectionTitles[section]
label.textColor = Utils.Color.COLOR_2B5034
label.font = UIFont.systemFont(ofSize: 16)
headerView.addSubview(label)
let leadingConstraint = NSLayoutConstraint(item: label, attribute: .leading, relatedBy: .equal, toItem: headerView, attribute: .leading, multiplier: 1, constant: 20)
let topConstraint = NSLayoutConstraint(item: label, attribute: .top, relatedBy: .equal, toItem: headerView, attribute: .top, multiplier: 1, constant: 30)
label.translatesAutoresizingMaskIntoConstraints = false
headerView.addConstraints([leadingConstraint, topConstraint])
return headerView
}
func numberOfSections(in tableView: UITableView) -> Int {
return 5
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
switch (section)
{
case 0:
return 1
case 1:
return getNumberOfRows(results: self.transactions_PND)
case 2:
return getNumberOfRows(results: self.transactions_PAY)
case 3:
return getNumberOfRows(results: self.transactions_RJC)
case 4:
return getNumberOfRows(results: self.transactions_DUE)
default:
return 0
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = Bundle.main.loadNibNamed("QueryCollectTableViewCell", owner: self, options: nil)?.first as! QueryCollectTableViewCell
cell.selectionStyle = .none
switch (indexPath.section)
{
case 0:
cell.strLblPhone.text = NSLocalizedString("strLblTitlePhone",comment:"")
cell.strLblPhone.textColor = Utils.Color.COLOR_2B5034
cell.strLblAmount.text = NSLocalizedString("strLblAmount",comment:"")
cell.strLblAmount.textColor = Utils.Color.COLOR_2B5034
cell.strLblDate.text = NSLocalizedString("strLblDate",comment:"")
cell.strLblDate.textColor = Utils.Color.COLOR_2B5034
cell.strLblReason.text = NSLocalizedString("strLblReason",comment:"")
cell.strLblReason.textColor = Utils.Color.COLOR_2B5034
cell.addBottomBorder(Utils.Color.COLOR_C2922D, height: 2, separation: 0)
return cell
case 1:
return getCell(cell: cell, resultTransactions :self.transactions_PND, row: indexPath.row)
case 2:
return getCell(cell: cell, resultTransactions :self.transactions_PAY, row: indexPath.row)
case 3:
return getCell(cell: cell, resultTransactions :self.transactions_RJC, row: indexPath.row)
case 4:
return getCell(cell: cell, resultTransactions :self.transactions_DUE, row: indexPath.row)
default:
return cell
}
}
For popular each of the sections I manage 4 different lists. I should not show the sections in case I do not have data to show.
This happens to me:
I would need only the list to have the scroll... I don't know if this is possible using a single UITableView.
The view you have with
Celular
,Monto
... instead of putting it astableview.tableHeaderView
, you have to put it outside the table. That is, in your viewController, you add that view on top, and below you add theUITableView
.Then you should set the style of your table to
plain
, like this:And so you would have the sticky headers,
Pendientes
andPagados
they would stay below the tab view that we mentioned in the first paragraph, while you scroll.So that the cells that go up are not seen below, you need to put a
backgroundColor
to the view of the header. In your method:And that should be enough for you.
So I'm left with @jdev's solution
My method is like this to give the styles to the titles of the sections:
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { let view = UIView() view.backgroundColor = UIColor.white
and configure the style of the table in the .xib in this way: