Tag Archive for 'UITableView'

Set background image of UITableView

How to set a background image of a UITableView is a very often and common question. Today we will try to answer this.

There are two ways to solve this problem:

  1. Create a new UIView – lets call the view V – set the background image of V and embed a UITableView – lets call it TV – within V. Now set the background color of TV to [UIColor clearColor] and do so for all UITableViewCells. Everything seems to work well but you have to handle some tasks (regarding the UITableView) on your own now like deselcting the currently selected UITableViewCell when returning from a prior pushed view.
  2. The second approach requires less effort but creates a not so pretty side-effect. You can simply create a new UIView, set its background image and send this view to the back of the UITableView. If you do so the background image scrolls with the UITableView. So this solution should only be used if you do not have to many cells, i.e. to create a simple menu screen in a grouped style within your app.

For those guys, implementing the second solution, the following code would be helpful. Put it i.e. in your - (void) viewDidLoad; method or further initialization of your view controller.

Continue reading ‘Set background image of UITableView’

UIButton in UITableView Footer

Once upon a time, there was a pretty UITableView within my iPhone user interface. This table included some UITableViewCells which were used to provide some information of a specific data record. At this time I thought that it would be nice to have a big red delete button at the end of the table (like we can locate it in the edit mask of the Apple Calendar App).

To realize such a button at the end of the table we can simply use the ability to define our own footer view for each section within a UITableView. The next steps describe how we can do that for a single section.

Continue reading ‘UIButton in UITableView Footer’

UITableViewCell with detail label

Creating some UITableViewCells with a main label and a secondary detail label (as we allready knew from the e.g. settings menu on the iPhone) to show some kind of a selected value isn’t very complicated but annoying anyway. The ways to do that were either to create a label as subview programmatically or you used the Interface Builder to provide a nib file including the cell.

This common task was greatly facilitated by the introduction of the new iPhone SDK 3.0, which now supports a few styles for UITableViewCells (see UITableViewCellStyle for more information). The following example shows how to create such a simple table cell:

//create a cell with the style UITableViewCellStyleValue1
UITableViewCell *cell = [[UITableViewCell alloc]
			 initWithStyle:UITableViewCellStyleValue1
			 reuseIdentifier:@"yourReuseIdentifier"];
 
//the main label will be shown on the left side
[cell.textLabel	setText:@"mainLabel"];
 
//the detail label will be shown on the right side
//in blue color, right-aligned and in a smaller font size
[cell.detailTextLabel setText:@"detailLabel"];

The result of this will be something like that:

tableviewcell_with_detail