0
votes

I have UITableView with height = 1000px, I need to set scrollable background. I know how to set background image for tableview but in this case that background image will not be scrollable.

Right now the only idea how to do that is: 1) create scroll view with proper height (about 1000 px), and set background image for that scroll view. then set frame of UITableView as the bounds of scroll view (and disable scrolling for UITableView).

But my idea is rather bad, because I have many cells with many images on them, and in my approach all the cells will stay in memory.

What is the best way how to implement scrollable background inside tableview?

P.S.

I have tableview header of unknown height (depends on the response from server)

2

2 Answers

1
votes

If it's a repeatable pattern, one way would be to split the image, repeat the middle part and show the top/bottom only when the contentOffset reaches the boundaries of contentSize. Or set the top/bottom parts only for first/last cell and the center (repeatable part) for every other.

Edit:

As I said, for a repeatable pattern:

Sample background image

You crop the elements you'd use for header, footer and middle

Sample cropping lines

Sample cropped 'components'

Of course, for header & footers with information in them you'd probably need bigger header/footer images:

Sample with bigger header & footer

Get the parts and assign them as backgrounds in your cellForRowAtIndexPath: method. You can do the whole thing with a single image without separating the cropped parts in different files, that's already discussed here.

0
votes

There is some example code in the official docs on how to make a "synchronized" scroll view. Since a table view is a scroll view, it should work to synchronize with a table view as well. So make your background be a scroll view behind the table view, and synchronize it.

The critical step is this:

[[NSNotificationCenter defaultCenter] addObserver:self
                     selector:@selector(synchronizedViewContentBoundsDidChange:)
                     name:NSViewBoundsDidChangeNotification
                       object:synchronizedContentView];

Then it's simple in your handler to match up the content offset of your background vs. the table view.