// // CellsViewController.h // Cells // // Created by h on 11-6-6. // Copyright 2011 __MyCompanyName__. All rights reserved. // #import <UIKit/UIKit.h> #define KNameValueTag 1 #define KColorValueTag 2 @interface CellsViewController : UIViewController <UITableViewDelegate,UITableViewDataSource>{ NSArray *computers; } @property (nonatomic,retain) NSArray *computers; @end // // CellsViewController.m // Cells // // Created by h on 11-6-6. // Copyright 2011 __MyCompanyName__. All rights reserved. // #import "CellsViewController.h" @implementation CellsViewController @synthesize computers; /* // The designated initializer. Override to perform setup that is required before the view is loaded. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; } */ /* // Implement loadView to create a view hierarchy programmatically, without using a nib. - (void)loadView { } */ // Implement viewDidLoad to do additional setup after loading the view, typically from a nib. - (void)viewDidLoad { NSDictionary *row1=[[NSDictionary alloc] initWithObjectsAndKeys:@"MacBook",@"Name",@"White",@"Color",nil]; NSDictionary *row2=[[NSDictionary alloc] initWithObjectsAndKeys:@"MacBook Pro",@"Name",@"2Silver",@"Color",nil]; NSDictionary *row3=[[NSDictionary alloc] initWithObjectsAndKeys:@"iMac",@"Name",@"White",@"Color",nil]; NSDictionary *row4=[[NSDictionary alloc] initWithObjectsAndKeys:@"Mac Mini",@"Name",@"White",@"Color",nil]; NSDictionary *row5=[[NSDictionary alloc] initWithObjectsAndKeys:@"Mac Por",@"Name",@"Silver",@"Color",nil]; NSArray *array = [[NSArray alloc] initWithObjects:row1,row2,row3,row4,row5,nil]; //NSMutableArray *array = [[NSMutableArray alloc] initWithObjects:row1,row2,row3,nil]; //[array add] self.computers=array; [row1 release]; [row2 release]; [row3 release]; [row4 release]; [row5 release]; [array release]; [super viewDidLoad]; } /* // Override to allow orientations other than the default portrait orientation. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { // Return YES for supported orientations return (interfaceOrientation == UIInterfaceOrientationPortrait); } */ - (void)didReceiveMemoryWarning { // Releases the view if it doesn't have a superview. [super didReceiveMemoryWarning]; // Release any cached data, images, etc that aren't in use. } - (void)viewDidUnload { // Release any retained subviews of the main view. // e.g. self.myOutlet = nil; self.computers=nil; } - (void)dealloc { [self.computers release]; [super dealloc]; } #pragma mark - #pragma mark Table Data Source Methods -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [self.computers count]; } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellTableIdentifier=@"CellTableIdentifier"; UITableViewCell *cell= [tableView dequeueReusableCellWithIdentifier:CellTableIdentifier]; if (cell==nil) { cell= [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellTableIdentifier] autorelease]; CGRect nameLabelRect= CGRectMake(0, 5, 70, 15); UILabel *nameLabel= [[UILabel alloc] initWithFrame:nameLabelRect]; nameLabel.textAlignment=UITextAlignmentRight; nameLabel.text=@"name:"; nameLabel.font=[UIFont boldSystemFontOfSize:12]; [cell.contentView addSubview:nameLabel]; [nameLabel release]; CGRect colorLableRect= CGRectMake(0, 26, 70, 15); UILabel *colorLable= [[UILabel alloc] initWithFrame:colorLableRect]; colorLable.textAlignment=UITextAlignmentRight; colorLable.text=@"color:"; colorLable.font= [UIFont boldSystemFontOfSize:12]; [cell.contentView addSubview:colorLable]; [colorLable release]; CGRect namevalueRect= CGRectMake(80, 5, 200, 15); UILabel *namevalue= [[UILabel alloc] initWithFrame:namevalueRect]; namevalue.textAlignment=UITextAlignmentRight; namevalue.tag=KNameValueTag; [cell.contentView addSubview:namevalue]; [namevalue release]; CGRect colorValueRect= CGRectMake(80, 25, 200, 15); UILabel *colorValue= [[UILabel alloc] initWithFrame:colorValueRect]; colorValue.textAlignment=UITextAlignmentRight; colorValue.tag=KColorValueTag; [cell.contentView addSubview:colorValue]; [colorValue release]; } NSUInteger row=[indexPath row]; NSDictionary *rowData= [self.computers objectAtIndex:row]; UILabel *name= (UILabel *)[cell.contentView viewWithTag:KNameValueTag]; name.text= [rowData objectForKey:@"Name" ]; UILabel *color=(UILabel *)[ cell.contentView viewWithTag:KColorValueTag]; color.text=[rowData objectForKey:@"Color"]; return cell; } /*- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return 66; }*/ @end