Good resources
Creating A Table View ViewController
These instructions include the table view option when creating the view controller files and provide a simpler means that adding a table view to a typical view controller (although there isn’t actually much difference between the two if creating a basic table view).
Menu > File > New > iOS > Cocoa Touch > Objective-C class
Press Next
Subclass of = “UITableViewController”
Uncheck “With XIB for user interface” unless you need it for some reason.
Name it ViewController_iPhone as your done. All of the methods are crated for you ready to use. To address the table view directly (i.e. in the init method to style it etc) use [self tableView] . See below for details on styling etc…
Adding Table View To A Normal ViewController
These instructions don’t require the table view option to have been checked when creating the view controller files.
In #ViewController.h
@interface #ViewController_iPhone : UIViewController
<UITableViewDelegate, UITableViewDataSource> //-ADD THESE DELEGATES
{
UITableView *tableView1; //-ADD THIS
}
In #ViewController.m
//*******************************
//*******************************
//********** LOAD VIEW **********
//*******************************
//*******************************
- (void)loadView
{
tableView1 = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]
style:UITableViewStyleGrouped]; //UITableViewStylePlain, UITableViewStyleGrouped
tableView1.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
tableView1.delegate = self;
tableView1.dataSource = self;
[tableView1 reloadData];
//[tableView1 setRowHeight:56];
self.view = tableView1;
//[tableView1 release];
}
//******************************************
//******************************************
//********** TABLE FILL DELEGATES **********
//******************************************
//******************************************
//***************************************************
//***** RETURN NUMBER OF SECTIONS IN TABLE VIEW *****
//***************************************************
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
//********************************************
//***** RETURN NUMBER OF ROWS IN SECTION *****
//********************************************
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 2; //e.g. ([[[MyClassName myArrayName] objectAtIndex:section] count]);
}
//**********************************************
//***** RETURN TITLE FOR HEADER OF SECTION *****
//**********************************************
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
//return nil; //No header should be shown
return @"My Title"; //e.g. [[[MyClassName myArrayName] objectAtIndex:section] objectAtIndex:0];
}
//*********************************
//***** DEFINE CELL FOR A ROW *****
//*********************************
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *MyIdentifier = @"MyIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil)
{
//Initialise the cell and set it's style
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier] autorelease];
}
//Set other cell properties
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
cell.textLabel.text = @"Cell Label"; //e.g.[[[MyClassName myArrayName] objectAtIndex:indexPath.section] objectAtIndex:(indexPath.row)];
return cell;
}
//******************************************
//******************************************
//********** TABLE - ROW SELECTED **********
//******************************************
//******************************************
- (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//e.g.:
/*
NSString *mySelectedName;
mySelectedName = [[[MyClassName myArrayName] objectAtIndex:indexPath.section] objectAtIndex:(indexPath.row + 1)];
if ([mySelectedName isEqual:@""])
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Sorry - not found"
message:nil
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
;
;
return;
}
*/
}
//*************************************
//*************************************
//********** VIEW DID UNLOAD **********
//*************************************
//*************************************
- (void)viewDidUnload
{
[super viewDidUnload];
[tableView1 release];
tableView1 = nil;
}
//*****************************
//*****************************
//********** DEALLOC **********
//*****************************
//*****************************
- (void)dealloc
{
[tableView1 release];
[super dealloc];
}
Row Height
Do this when creating the table view
[tableView setRowHeight:40];
Or use the delegate heightForRowAtIndexPath to have different heights for different cells.
Disclosure Button
(Right Hand button)
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
Bespoke Accessory View
(Bespoke image on right side)
//----- ACCESSORY VIEW -----
if (SomeTest)
{
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
UIImage *AccessoryImage = [UIImage imageNamed:@"SomeImage.png"];
UIButton *AccessoryButton = [UIButton buttonWithType:UIButtonTypeCustom];
CGRect AccessoryFrame = CGRectMake(0.0, 0.0, AccessoryImage.size.width, AccessoryImage.size.height);
AccessoryButton.frame = AccessoryFrame;
[AccessoryButton setBackgroundImage:AccessoryImage forState:UIControlStateNormal];
//[AccessoryButton addTarget:self action:@selector(checkButtonTapped:event:) forControlEvents:UIControlEventTouchUpInside];
AccessoryButton.backgroundColor = [UIColor clearColor];
cell.accessoryView = AccessoryButton;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
cell.accessoryView = UITableViewCellAccessoryNone;
}
Setting Smaller Line Of Text Below / Subtitle Text
//Use Subtitle Style:
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:MyIdentifier] autorelease];
//Set the text:
cell.detailTextLabel.text = (NSString *)MyString;
Setting An Image
A left side image
UIImage *cellImage = [UIImage imageNamed:@"SomeImage.png"];
cell.imageView.image = cellImage;
Using Bespoke Background
Do this when creating the table view
//Set Background to use color
[tableView1 setBackgroundColor:[UIColor colorWithRed:2/255.0 green:2/255.0 blue:2/255.0 alpha:1]];
//[tableView1 setBackgroundColor:[UIColor clearColor]];
//Set Background to use image
UIImageView *background = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"BackgroundCurrent_iPhone.png"]];
[tableView1 setBackgroundView:background];
[background release];
self.view = tableView1;
//[tableView1 release];
Another approach
UIImageView *background = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"background.png"]];
[self.view addSubview:background];
[background release];
[myTableView setRowHeight:68];
[myTableView setBackgroundColor:[UIColor clearColor]];
[myTableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
[myTableView setIndicatorStyle:UIScrollViewIndicatorStyleWhite];
Setting Line Style
//Set seperator line style
[tableView1 setSeparatorColor:[UIColor colorWithRed:60/255.0 green:60/255.0 blue:60/255.0 alpha:1]];
[tableView1 setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine]; //Single line shown
//[tableView1 setSeparatorStyle:UITableViewCellSeparatorStyleNone]; //No lines shown
//[tableView1 setSeparatorStyle:UITableViewCellSeparatorStyleSingleLineEtched]; //Single lines shown for end of table filler cells only (blank space if table doesn't fill screen)
Setting Indicator Style
[tableView1 setIndicatorStyle:UIScrollViewIndicatorStyleWhite];
Setting Text Style
cell.textLabel.textColor = [UIColor colorWithRed:230/255.0 green:230/255.0 blue:230/255.0 alpha:1];
cell.detailTextLabel.textColor = [UIColor colorWithRed:230/255.0 green:230/255.0 blue:230/255.0 alpha:1];
cell.textLabel.font = [UIFont boldSystemFontOfSize:18];
cell.detailTextLabel.font = [UIFont systemFontOfSize:14];
Stopping A Cell Being Highlighted When Selected
cell.selectionStyle = UITableViewCellSelectionStyleNone;
Graphic Styling Of Tables
Core Graphics 101: Lines, Rectangles, and Gradients
http://iphonedevelopertips.com/user-interface/creating-unique-looking-tables-with-custom-cells.html