views:

126

answers:

1

I am trying to create a UITableView that only displays the number of rows that have data content in them. This is a menu system, and suppose there are 5 choices in the particular menu, I only want to show 5 rows on the screen, with a background image in place of where the empty rows would be (A similar example I guess would be the World Clock). How should I go about this? I've trawled around but can't find anything that helps. Many thanks.

---------------------
Menu Item 1
---------------------
Menu Item 2
---------------------
Rest of view as image




---------------------

Using a grouped table view works, but I don't really want to use this.

+1  A: 

Your UITableView can have a backgroundView. Set it to be a UIImageView, and set it's frame to the table view's bounds. This code is untested, but should give you an idea.

UIImage * img = [UIImage imageNamed:@"myImage.jpg"];
UIImageView * imgView = [UIImageView initWithImage:img];
imgView.frame = myTableView.bounds;
myTableView.backgroundView = imgView;

EDIT: The above will produce a background like world clock. to make it show below the other cells as a cell, change your data source:

- numberOfRowsInSection:section: should return one more than before. - tableView:canEditRowAtIndexPath: and tableView:canMoveRowAtIndexPath: should return NO for this new cell. - (UITableViewCell *)tableView:cellForRowAtIndexPath: needs to create the new cell when the it is asked for, so if you have 5 data cells, the 6th will return:

UITableViewCell * cell = //create your cell
//change the height to match the remaining space
UIImage * img = [UIImage imageNamed:@"myImage.jpg"];
UIImageView * imgView = [UIImageView initWithImage:img];
cell.backgroundView = imgView;

I am not sure if you have to set the image view size, but you do have to set the cell size to take up the remaining space, with something like this in your table view delegate's tableView:heightForRowAtIndexPath: for the final row:

//if final row
return myTableView.bounds.size.height - 16.0 * myData.count; //modify from the default 16.0 as needed.
Peter DeWeese
Looking at that code, it looks like that will add an image behind the table views cells. What I am trying to achieve is something like I have updated my question to show. Correct me if I am wrong.
churchill614
Alright. My suggestion was to make it like the world clock, which uses a background image, and this will do so. If you want the image to show below the rest of the cells, you need to make it a cell! I'll edit my comment.
Peter DeWeese