Reputation: 1074
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()
}
Upvotes: 13
Views: 15843
Reputation: 522
If you have created your TableView programmatically you can make it grouped in the initialser itself. So the section header won't be sticky.
let tableView = UITableView(frame: .zero, style: .grouped)
Upvotes: 0
Reputation: 2470
Do following steps.
From @IB
Plain
by defaultGrouped
Or if you are creating TableView Programmatically use
let tableView = UITableView(frame: YourFrame, style: .Grouped)
Upvotes: 28
Reputation: 29
Step1: Go to Mainstoryboard
Step2: Click on Your Tableview
Step3: Go to Attribute Inspector
Step4: Find Style Attribute
drop down which is Plain
by default
Step5: Change it to Grouped
Upvotes: 2
Reputation: 1312
Set UITableViewStyle
to UITableViewStyleGrouped
, the headers will scroll up with the cells.
and
func tableView(_ tableView: UITableView,
heightForFooterInSection section: Int) -> CGFloat
{
return 0.000001;
}
Upvotes: 3
Reputation: 568
Instead of UIHeaderViewCell
, You should use a UITableViewCell
. Make section header height as 0 and for every 0th item in that section , display a UITableViewCell
with heading on it
Upvotes: 0
Reputation: 1564
Some possible solutions :
Upvotes: 2