0

I have implemented a tableview (4 sectional) in IOS. Problem is that I have just added a section header in first section.Other sections don't have a header.First section does not have row (number of rows is 0).other sections have multiple rows.When I scroll , first section's header is not sticky.it is scrolling and out of screen.My table view style is plain.How can I want to make first section's header is always sticky.Code is below.Unfortunatelly tableview is so complicated and I don't want to make it only one section so that I have implemented it multi sectional.

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    if section == 0 {
        return tabHeaderView.contentView
    }
    else {
        return UIView()
    }
}

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    if section == 0 {
      return UITableView.automaticDimension
    }
    else {
        return 0 //just first section have a header (tabview)
    }
}

func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
    return CGFloat.leastNormalMagnitude
}

func numberOfSections(in tableView: UITableView) -> Int {
    if dataReady {
        totalSectionCount = getSectionCount()
        return totalSectionCount
    }
    else {
        return 1 //permanent because of tabview
    }
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
    if !dataReady || ApplicationContext.instance.userAuthenticationStatus.value == .semiSecure{
        return 1//for shimmer cell and semisecure view
    }
    else {
        return getNumberOfRows(sectionNumber: section)
    }
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    if ApplicationContext.instance.userAuthenticationStatus.value == .semiSecure {
        semisecureViewCell = EarningsSemisecureViewCell()
        setSemisecureViewTextInfo(cell: semisecureViewCell)
        semisecureViewCell.delegate = self
        semisecureViewCell.layoutIfNeeded()
        return semisecureViewCell
    }
    else if !dataReady {
        return getShimmerCell()
    }
    else {
        if indexPath.row == 0 && !(selectedTabIndex == .BRANDPOINT && indexPath.section == 1){//marka puan listesinde header cell olmayacak
            return getSectionHeaderViewCell(tableView: tableView,sectionNumber: indexPath.section)
        }
        else {
            let cell = getTableViewCell(tableView: tableView, indexPath: indexPath)
            cell.layoutIfNeeded()
            return cell
        }
    }
}
Kuldeep
  • 4,466
  • 8
  • 32
  • 59
ahmetvefa53
  • 581
  • 6
  • 20

3 Answers3

2

Each section header will stick to the top until that section has some rows to display. Once you scroll all the rows for the section up. section header will be replaced by the next section header.

Here you can use one of the two solutions.

  1. Instead of table view section header. Put your view on top of UITableView.
  2. You can use only one section and combine all the rows in it.
rv7284
  • 1,092
  • 9
  • 25
  • I have also a tableheaderview.I think you are right about it.I must combine all in one section.Thank you so much :) – ahmetvefa53 Dec 24 '18 at 10:50
  • Solution 1 is nice. :) Put your view on top of tableView, or add the view to the same parent view of tableView. – AechoLiu Dec 25 '18 at 05:13
1

From how you are describing your setup, I tend to believe that what you are looking for is the tableHeaderView of the tableView, not the section header.

See either this question, or official documentation for more info.

If that does not meet your requirements, you might wanna consider a custom view on top of the tableView, as is described here.

Milan Nosáľ
  • 19,169
  • 4
  • 55
  • 90
0

If the first section must always be sticky when any section is displaying, I will consider to put a custom view on top of tableView. Use autoLayout or UIStackView to let it work as a table header view.

AechoLiu
  • 17,522
  • 9
  • 100
  • 118