swift UITableView is it possible to set the header of the table and the header of the sections?

0

I have a table with a header and several sections (each with a header) and I would need both the header of the table and the header of the sections to be fixed ... is that possible using a UITableView?

Then I leave the code of 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
        }
    }

To popularize each of the sections I manage 4 different lists. I do not have to show the sections if I do not have data to show.

This happens to me:

I would need the scroll to have only the list ... I do not know if this is possible using a single UITableView.

    
asked by Tamara Arleo 19.04.2018 в 20:51
source

2 answers

0

So I'm left with @jdev's solution

My method is this way 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

    let label : UILabel = UILabel()
    label.text = sectionTitles[section]
    label.frame = CGRect(x: 20, y: 5, width: 200, height: 30)
    label.textColor = Utils.Color.COLOR_2B5034
    view.addSubview(label)
    return view
}

and the style of the table is configured in the .xib in this way:

    
answered by 23.04.2018 / 16:44
source
2

The view you have with Celular , Monto ... instead of putting it as tableview.tableHeaderView , you have to put it out of the table. That is, in your viewController, you add that view at the top, and below you add UITableView .

Then you should set the style of your table to plain , like this:

tableview.style = .plain

And so you would have the sticky headers, Pendientes and Pagados that would stay below the view of tabs that we mentioned in the first paragraph, while you scroll.

So that you do not see below the cells that go up, you need to put a backgroundColor in the header view. In your method:

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let headerView: UIView = UIView(frame: CGRect(x: 20, y: 0, width: 100, height: 60))
    headerView.backgroundColor = .white
    ...
}

And with that should be enough.

    
answered by 23.04.2018 в 09:37