//
//  TableViewsViewController.m
//  TableViews
//
//  Created by Axit Patel on 9/2/10.
//  Copyright Bayside High School 2010. All rights reserved.
//

#import "TableViewsViewController.h"

@implementation TableViewsViewController

#pragma mark -
#pragma mark Synthesizers

@synthesize table;
@synthesize sitesArray;
@synthesize imagesArray;

#pragma mark -
#pragma mark View lifecycle

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
	
	
	// Load up the sitesArray with a dummy array : sites
	NSArray *sites = [[NSArray alloc] initWithObjects:@"Active Tuts", @"AE Tuts", @"Audio Tuts", @"Mobile Tuts", @"Net Tuts", @"Photo Tuts", @"PSD Tuts", @"Vector Tuts", nil];
	self.sitesArray = sites;
	[sites release];
	
	UIImage *active = [UIImage imageNamed:@"active.png"];
	UIImage *ae = [UIImage imageNamed:@"ae.png"];
	UIImage *audio = [UIImage imageNamed:@"audio.png"];
	UIImage *mobile = [UIImage imageNamed:@"mobile.png"];
	UIImage *net = [UIImage imageNamed:@"net.png"];
	UIImage *photo = [UIImage imageNamed:@"photo.png"];
	UIImage *psd = [UIImage	imageNamed:@"psd.png"];
	UIImage *vector = [UIImage imageNamed:@"vector.png"];
	
	NSArray *images = [[NSArray alloc] initWithObjects: active, ae, audio, mobile, net, photo, psd, vector, nil];
	self.imagesArray = images;
	[images release];
	
    [super viewDidLoad];
}

#pragma mark -
#pragma mark Table View datasource methods

// Required Methods

// Return the number of rows in a section
-(NSInteger) tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section
{
	return [sitesArray count];
}

// Returns cell to render for each row
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
	static NSString *CellIdentifier = @"CellIdentifier";
	
	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
	
	if (cell == nil) {
		cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
	}
	
	// Configure cell
	
	NSUInteger row = [indexPath row];
	
	// Sets the text for the cell
	cell.textLabel.text = [sitesArray objectAtIndex:row];
	
	// Sets the imageview for the cell
	cell.imageView.image = [imagesArray objectAtIndex:row];
	
	// Sets the accessory for the cell
	cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
	
	// Sets the detailtext for the cell (subtitle)
	cell.detailTextLabel.text = [NSString stringWithFormat:@"This is row: %i", row + 1];
	
	return cell;
}

// Optional

// Returns the number of section in a table view
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
	return 1;
}

#pragma mark -
#pragma mark Table View delegate methods

// Return the height for each cell
-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
	return 50;
}

// Sets the title for header in the tableview
-(NSString *) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
	return @"Envato Tutsplus Sites";
}

// Sets the title for footer
-(NSString *) tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
{
	return @"Envato Rocks!";
}

// Sets the indentation for rows
-(NSInteger) tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath
{
	return 0;
}

// This method is run when the user taps the row in the tableview
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
	UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Tapped row!" 
											  message:[NSString stringWithFormat:@"You tapped: %@", [sitesArray objectAtIndex:indexPath.row]]
											  delegate:nil 
											  cancelButtonTitle:@"Yes, I did!" 
										      otherButtonTitles:nil];
	[alert show];
	[alert release];
	[tableView deselectRowAtIndexPath:indexPath animated:YES];
}

#pragma mark -
#pragma mark Memory management

- (void)didReceiveMemoryWarning {
	NSLog(@"Memory Warning!");
    [super didReceiveMemoryWarning];
}

- (void)viewDidUnload {
	self.table = nil;
	self.sitesArray = nil;
	self.imagesArray = nil;
	[super viewDidUnload];
}


- (void)dealloc {
	[table release];
	[sitesArray release];
	[imagesArray release];
    [super dealloc];
}

@end
