Is it possible to have a fixed uitableview Header while using sections?
There’s no way to maintain the header of a tableView
fixed, but
an useful approach when you need a unique header, is to use a UIViewController
rather than a UITableViewController
, and set the header (UIView
) out from the tableView
.
Something like this:
UITableView with fixed section headers
The headers only remain fixed (floating) when the UITableViewStyle
property of the table is set to UITableViewStylePlain
.
If you have it set to UITableViewStyleGrouped
, the headers will scroll up with the cells (will not float).
UITableview: Fixed Section Headers
You should add separate tableViews instead of sections in same tableView. That way your tableView's frame will be fixed and adding row to either won't push others frame down.
Ungrouping UITableView Section Header when Header is top of view
I am not completely sure I follow what you are trying to accomplish, but please allow me to attempt to infer and feel free to provide clarifications. I like the table view practice :)
As I understand it:
1) You want the white view, the red view, and the table cells beneath the red view to scroll upward from the default position
2) You want the white view to scroll out of visibility
3) You want the red view to float at the top while the cells scroll beneath it
4) You currently have the white view as a table header, the red view as a section header, and the gray area are table cells
Sounds like a cool app!
Why not use 2 table sections:
1) Drop the table header
2) Set the white view in cell 0 section 0 of the table view.
3) Set table delegate methods so section 0 will have a nil header with 0 height
3.5) Also set the table delegate methods so section 1 will be your main table
4) Use UITableViewStylePlain so section 1 header will float at the top
Is this the desired functionality?
UITableView Section Header Stop Position
I think this should work:
override func scrollViewDidScroll(_ scrollView: UIScrollView)
{
super.scrollViewDidScroll(scrollView)
let inset: CGFloat = 73
if scrollView.contentOffset.y < inset && scrollView.contentOffset.y > 0 {
scrollView.contentInset = UIEdgeInsets(top: scrollView.contentOffset.y, left: 0, bottom: 0, right: 0)
} else {
if scrollView.contentOffset.y < 0 {
scrollView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
} else {
scrollView.contentInset = UIEdgeInsets(top: inset, left: 0, bottom: 0, right: 0)
}
}
}
Fix tableHeaderView while scrolling UITableViewController with multiple section
I am answering my question, the easy way is to add the search in the navigation bar.
if #available(iOS 11.0, *) {
...
let searchController = UISearchController(searchResultsController: yourResultController) // Search Controller
navigationItem.hidesSearchBarWhenScrolling = false
navigationItem.searchController = searchController
}
This replicate the behavior of the Contact list.
Some other intersting read on how to customize it: http://iosrevisited.blogspot.be/2017/11/add-search-controller-in-navigation-bar-swift.html
Customize UITableView header section
You can try this:
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 18)];
/* Create custom view to display section header... */
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, tableView.frame.size.width, 18)];
[label setFont:[UIFont boldSystemFontOfSize:12]];
NSString *string =[list objectAtIndex:section];
/* Section header is in 0th index... */
[label setText:string];
[view addSubview:label];
[view setBackgroundColor:[UIColor colorWithRed:166/255.0 green:177/255.0 blue:186/255.0 alpha:1.0]]; //your background color...
return view;
}
How to set specific section header in UITableView [Swift]
Use switch-case makes better code
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
switch section {
case 0: return "Section 0"
case 1: return "Section 1"
default: return nil
}
}
Related Topics
How to Debug an Issue with a Release Mode Build in iOS
How to Animate Uitableviewcell Height Using Auto-Layout
Change App Language in iOS Without Restarting the App
iOS Uicollectionview - Default Flow, Fill Rows from Right to Left
Uisearchdisplaycontroller - How to Preload Searchresulttableview
Skmutablepayment Property: Applicationusername
Intercept Request with Wkwebview
Swift: Natively Detect If App Has Crashed
How to Set the Tab Order in iOS
Connecting to a Bluetooth Device from iOS, No Mfi
Uitableview Invoke Swipe Actions Programmatically
Firebase Retrieving Data in Swift
How to Insert Cell in Uicollectionview Programmatically
How to Keep the Keyboard from Covering My UI Instead of Resizing It
Autosizing Cells: Cell Width Equal to the Collectionview
How to Use Mbprogresshud with Swift