1

I've added an image to a UITableview using the following code. What I can't figure out is how to have that background image scroll with the table (not an image in each cell, but a large image behind the UITable). I need the background image to be scrollable and in synch with the UITable scrolling.

I've searched and all the examples I've seen simply add a static image.

here's the code:

[self.view setBackgroundColor:
 [UIColor colorWithPatternImage:
  [UIImage imageWithContentsOfFile:
   [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:
    @"background.png"]]]];

thanks for any help.

hanumanDev
  • 6,592
  • 11
  • 82
  • 146

2 Answers2

3

I had this same problem and didn't find any solutions. I rolled my own which I explain in more detail than can be easily given here. It's explained at UITableView that scrolls the background with dynamic content

The basic idea is the following:

  • create a second table view behind your table view that has dynamic cell heights (or heights that aren't a multiple of a background pattern image's height)
  • turn its .userInteractionEnabled property to NO (so it doesn't receive touch events)
  • have it listen to the 'front' table view's contentOffset property via KVO so that the background table view sets its contentOffset to the same value.
  • have this background table view be its own delegate (you have to make a subclass so to implement KVO listening handlers anyway), and it populates empty cells' contentView.backgroundColor = [UIColor colorWithPatternImage: ...]
  • make sure your 'front' table view cells have transparent backgrounds. i.e. backgroundView set to an imageView without an image and opaque set to NO, and backgroundColor set to clearColor.

worked for me quite fine, and didn't seem in any way slower. The tables were still quite snappy on an iPod 4G

horseshoe7
  • 2,745
  • 2
  • 35
  • 49
1

Because few days ago I saw that -

http://www.appuicomponents.com/component/sbtv

I am guessing the answer is not trivial. But maybe you would like to try that ?

Any way, if you are using a pattern image, isn't that possible to add the pattern to the cells them selfs ?

shannoga
  • 19,649
  • 20
  • 104
  • 169
  • the image is a large image and adding it to each individual cell won't work. I'll check out the link, thanks – hanumanDev Jun 05 '11 at 14:34
  • You might try to divide the image to 2 or 3 images and then apply it to the cells - first image --> first cell, second image --> second cell etc.... – shannoga Jun 05 '11 at 14:59
  • I was thinking that might be an option. I haven't done that before though, so I'll have to do some searching. thanks. – hanumanDev Jun 05 '11 at 15:12
  • here's a how to in case anyone else might need something similar: http://stackoverflow.com/questions/5812261/uitableviewcell-issue-indexpath-row-not-displaying-correctly – hanumanDev Jun 05 '11 at 15:15