Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableView(Controller) - Hide empty rows

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.

like image 276
Ashley Bye Avatar asked Sep 08 '10 21:09

Ashley Bye


1 Answers

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 alloc] 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 alloc] 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.
like image 53
Peter DeWeese Avatar answered Oct 05 '22 23:10

Peter DeWeese