iOS - Sticky section headers in grouped UITableView with custom cells - ios

I noticed that when using custom cells in a grouped UITableView, the section headers no longer stay at the top of the view when scrolling down. They just pass out of the view like the rest of the cells. Is there a way to keep the default behavior of the headers while using a custom cell?

grouped style never stays at the top, plain does.

Related

How to disable the scrolling of table view for first Section #iOS

I have two sections in my table view , I want to have scroll enabled for 2nd section and disabled for 1st section , how could I achieve it ?
A UITableView as a whole is a ScrollView so there is no way to only scroll one section. I'd suggest moving what is in the first section into a UIView, then placing a UITableView below that UIView that just has a single section that contains the views you had in the section of the view.
This way the top part will not scroll, but the bottom part will.
A way to workaround it is having 2 UITableViews, one with scrolling disabled while the other has it enabled.
You can disable scrolling on a UITableView on the Attributes inspector while it's selected. There's a whole section on the Scroll View the UITableView has, and in the second subsection there's a toggle for "Scrolling Enabled" which is on by default.
[Edit: I've just seen the other comment, and I'd still recommend 2 UITableViews if you are dealing with dynamic content. If the content from the 1st section isn't dynamic, you may use an UIView as it'll save a lot of time just by not needing to implement 2 UITableViews in a single UIViewController]
#vinay
Try this solution
Take a view at the top of view controller.
Below that view take tableview.
Set table style to Grouped. It makes sections of tableview scrollable.
In your case I would use Section Header for the second section and make that header as customView.
You need to call the following functions:
1) viewForHeaderInSection
if section == 2 {
... customize your header view
}
2) heightForHeaderInSection
if section == 2 {
... set height
}

How to make header only stick on negative content offset - Swift

I have a tableView and I have a UIView as the tableView header. I want the header to move up the screen when I scroll down and act just like any normal cell. But when I have reached the top of the view, where if I scroll up it will just bounce back, I want the tableView header to stick to the top.
One example is like the Facebook app headers. They scroll when you scroll, but if you're at the top of the page and you scroll up, they wont scroll down, they stay stuck to the top.
How do I achieve this?
If you're using a normal tableView (not grouped style) and you only have one section, you can accomplish this by using the section header instead. The default behaviour of this type of tableView is as you described. You'll just have to provide a custom view for the section header that looks like your cell.
If this isn't a possibility, you'll need to take advantage of tableView being a subclass of scrollView, and implement the scrollViewDidScroll delegate method to check content offsets etc.

Single Header View at top of UICollectionView with multiple sections

I understand how to include a header view per section, but how do I go about having a single header view at the top of a UICollectionView with multiple sections? I just want a view that sits on top of the collectionview that scrolls with the collectionview.
I've looked into decoration/supplementary views..but I think I can also just make it a new section with a single cell. What is the best practice for this?
Not sure if this is "best practice," but I ended up creating a section at the top with a single cell. I looked into supplementary views, but it seemed like those were primarily used as section headers and footers. New section at top with single item worked well for my purposes.

UITableView: anchor section header

I have a UITableView with one section and a header. The problem is that when I scroll down beyond the contents of the table, the header is pulled down along with the cells. I would like it to stay at the top of the view. I tried the suggestion on this post,
Change Default Scrolling Behavior of UITableView Section Header
using the footer of a zero-row section as the header, but it still isn't anchored. Does anyone know how to do this properly?
Are you using a UITableViewController? If you are using a UIViewController that contains a UITableView you could just add a UIView above the UITableView.
You could also add the UIView on top of the UITableView and add an alpha gradient to make it look more similar to a regular UITableView section header.

How to prevent section from scrolling when dragging cells?

I want sections would have same behavior as cells when I drag it.
Usual behavior is — when dragging cells, section stands on a same place until it meets other section.
What I want is section drags as usual cell.
The following is from the UITableView Class Reference
Table views can have one of two styles, UITableViewStylePlain and
UITableViewStyleGrouped. When you create a UITableView instance you
must specify a table style, and this style cannot be changed. In the
plain style, section headers and footers float above the content if
the part of a complete section is visible.
If you are going to use the UITableViewStylePlain, I am not sure you can re-engineer the section headers to not float.
An alternative is to use the UITableViewStyleGrouped or use UITableViewStylePlain and instead of using sections, you could separate out your table view by placing the section as another cell.

Resources