views:

698

answers:

4

Hi, I'm using a UITableView to layout content 'pages'. I'm using the headers of the table view to layout certain images etc. and I'd prefer it if they didn't float but stayed static as they do when the style is set to UITableViewStyleGrouped.

Other then using UITableViewStyleGrouped, is there a way to do this? I'd like to avoid using grouped as it adds a margin down all my cells and requires disabling of the background view for each of the cells. I'd like full control of my layout. Ideally they'd be a UITableViewStyleBareBones, but I didn't see that option in the docs...

Many thanks,

A: 

you can easily achieve this by implementing the viewForHeaderInSection method in the tableview delegate class. this method expects a UIView as return object (which is your header view). i have done this same thing in my code

Raj
I have also done this, the view that is returned still floats when you scroll...
Tricky
The original poster doesn't want a custom header. They want a header that doesn't "float" at the top of the table. Your suggestion still floats.
jemmons
A: 

You should be able to fake this by using a custom cell to do your header rows. These will then scroll like any other cell in the table view.

You just need to add some logic in your cellForRowAtIndexPath to return the right cell type when it is a header row.

You'll probably have to manage your sections yourself though, i.e. have everything in one section and fake the headers. (You could also try returning a hidden view for the header view, but I don't know if that will work)

frankodwyer
Yeah... it seems a bit of a hack though. This seems like an oversight an Apple's part, as clearly the code is there to do it, we just need to be able to set a flag.
Tricky
A: 

I've described the private methods that turns of section headers floating in my blog

Basically, you just need to subclass UITableView and return YES in two of them:

  • (BOOL) allowsHeaderViewsToFloat;
  • (BOOL) allowsFooterViewsToFloat;
XAK
An answer doesn't deserve a downvote just because it references private APIs (not everybody is writing code to be submitted to the AppStore). This is doubly true when the answer happens to be correct. Subclassing (or, more easily, making a UITableView category) that returns NO for these methods stops headers from floating.If you'd like to see this added as a public API, please file a bug report: http://bugreport.apple.com
jemmons
A: 

Ignore XAK. Do not explore any private methods if you want your app to have the chance of being accepted by apple.

This is easiest if you are using Interface Builder. You would add a UIView at the top of the view (where the images will go), then add your tableview below that. IB should size it accordingly; that is, the top of the tableview touches the bottom of the UIView you've just added and it's height covers the rest of the screen.

The thinking here is that if that UIView is not actually part of the table view, it will not scroll with the tableview. i.e. ignore the tableview header.

If you're not using interface builder, it's a little more complicated because you've got to get the positioning and height correct for the tableview.

imnk
This doesn't answer the question. Tricky is referring to the titleHeaderViews which are the views that sit between the cells, not the table header view which is what you seem to be suggesting.
Daniel Wood