Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent tableview section head from sticking while scrolling?

I have a tableview with many sections. I am using an actual tableview cell which I dequeue to use as a custom section header. The issue I am having is when I scroll the section header "sticks" to the top of the table until the next section appears.

How can I prevent this from happening and actually have it scroll up like normal?

Here is my code

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    if section == 3 {
        let cell = tableView.dequeueReusableCell(withIdentifier: "headerCell") as! HeaderTableViewCell
        return cell
    }
    return UIView()
}
like image 631
user7097242 Avatar asked Nov 17 '17 22:11

user7097242


3 Answers

Do following steps.

From @IB

  1. Select your tableView
  2. Go to Attribute Inspector
  3. Find Style Attribute drop down which is Plain by default
  4. Change it to Grouped

Or if you are creating TableView Programmatically use

let tableView = UITableView(frame: YourFrame, style: .Grouped)
like image 67
Ajay saini Avatar answered Nov 04 '22 21:11

Ajay saini


Change UITableViewStyle to UITableViewStyleGrouped.

like image 4
Fahad Azeem Avatar answered Nov 04 '22 21:11

Fahad Azeem


Set UITableViewStyle to UITableViewStyleGrouped, the headers will scroll up with the cells. and

func tableView(_ tableView: UITableView, 
heightForFooterInSection section: Int) -> CGFloat
{
    return 0.000001;
}
like image 3
Syed Sadrul Ullah Sahad Avatar answered Nov 04 '22 19:11

Syed Sadrul Ullah Sahad