• 25th, Nov 2010

Table View Controller – Sectioned

iPhone Sectioned Table View
MovieTableController.h

//  Created by Ignacio Pascual on 25/11/2010.
//
#import <UIKit/UIKit.h>

@interface MovieTableController : UITableViewController <UITableViewDelegate, UITableViewDataSource> {
	IBOutlet UITableView *uiTableView;
	NSMutableArray *listOfItems;

}
@property (nonatomic, retain) IBOutlet UITableView *uiTableView;
@property (nonatomic, retain) NSMutableArray *listOfItems;

@end

MovieTableController.m

//  Created by Ignacio Pascual on 25/11/2010.
//

#import "MovieTableController.h"

@implementation MovieTableController

@synthesize uiTableView, listOfItems;

- (void)viewDidLoad {
    [super viewDidLoad];
	//Customizing Table View
    uiTableView.rowHeight = 45;

    uiTableView.backgroundColor = [UIColor blueColor];
    uiTableView.separatorColor = [UIColor yellowColor];

	//Initialize the array.
	listOfItems = [[NSMutableArray alloc] init];

	NSArray *moviesBatch1Array = [NSArray arrayWithObjects:@"Paranormal Activity", @"Nine", @"Salvage", @"The Step Gather", nil];
	NSDictionary *moviesBatch1InDict = [NSDictionary dictionaryWithObject:moviesBatch1Array forKey:@"Movies"];

	NSArray *moviesBatch2Array = [NSArray arrayWithObjects:@"The Dark Night", @"Where the Wild things Are", nil];
	NSDictionary *moviesBatch2InDict = [NSDictionary dictionaryWithObject:moviesBatch2Array forKey:@"Movies"];

	[listOfItems addObject:moviesBatch1InDict];
	[listOfItems addObject:moviesBatch2InDict];

	//Set the title
	self.navigationItem.title = @"The Show";
}

// Cell: number of rows
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	NSDictionary *dictionary = [listOfItems objectAtIndex:section];
	NSArray *array = [dictionary objectForKey:@"Movies"];
	return [array count];
}

// Cell: style and data
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	static NSString *CellIdentifier = @"Cell";

	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
	if (cell == nil) {
		//Single Cell
		cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
		// or with detail text
		//cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
	}

	// Set up the cell...
	cell.textLabel.textColor = [UIColor whiteColor];
	cell.textLabel.font = [UIFont boldSystemFontOfSize:12.0];
	// Set up the detail text
	//cell.detailTextLabel.textColor = [UIColor grayColor];
	//cell.detailTextLabel.font = [UIFont boldSystemFontOfSize:13.0];
    //cell.detailTextLabel.textAlignment = UITextAlignmentCenter;

	// Set the text
	NSDictionary *dictionary = [listOfItems objectAtIndex:indexPath.section];
	NSArray *array = [dictionary objectForKey:@"Movies"];
	cell.textLabel.text = [array objectAtIndex:indexPath.row];
	//cell.detailTextLabel.text = @"Touch here for details";

	return cell;
}

// Cell: didSelectRowAtIndexPath
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	//Push Detail View Controller...
}

// Sections: number
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
	return [listOfItems count];
}

// Sections: style and data
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
	UIView *tempView = [[[UIView alloc]initWithFrame:CGRectMake(0,0,300,30)]autorelease];
	tempView.backgroundColor = [UIColor darkGrayColor];

	UILabel *tempLabel = [[UILabel alloc]initWithFrame:CGRectMake(10,0,300,30)];
	tempLabel.backgroundColor = [UIColor clearColor];
	tempLabel.font = [UIFont boldSystemFontOfSize:14.0];
	tempLabel.textColor = [UIColor whiteColor];

	if(section == 0)
		tempLabel.text=@"24 April 2010";
	else
		tempLabel.text=@"19 April 2010";

	[tempView addSubview:tempLabel];
	[tempLabel release];

	return tempView;
}

// Sections: height
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
	return 30;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (void)dealloc {
    [super dealloc];
}
@end

xCode Interface Builder Table View links

Tags: ,

Leave a Reply

*

© 2010 unexpected[it]. All Rights Reserved.