How to Change Table Cell Color Alternatively in iPhone

Introduction

In this article I will create a Single View application. Here I will add a table view using outlet and connect it's delegate and datasource to the file owner. To change the table cell color alternatively here we use the "-tableView willDisplayCell:method.

To better understand it we use the following procedure.

Step 1

Open XCode by double-clicking on it.

Step 2

Create a New XCode Project by clicking on it.

Step 3

Now Select Single View Application and click on "Next".

Step 4

Now provide your Product Name and Company Identifier. 

Step 5

Select the location where you want to save your project and click on "Create".

Step 6

Now we write the code of the Objective-C classes:

ViewController.h

#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
{
IBOutlet UITableView *table;
NSMutableArray *array;
}
@property(strong,nonatomic)IBOutlet UITableView *table;
@property(strong,nonatomicNSMutableArray *array;
@end

ViewController.m

#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize table,array;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// [[self table] setBackgroundColor:[UIColor yellowColor]];
array = [[NSMutableArray alloc]init];
[array addObject:@"Sun"];
[array addObject:@"Mon"];
[array addObject:@"Tues"];
[array addObject:@"Wed"];
[array addObject:@"Thur"];
[array addObject:@"Fri"];
[array addObject:@"Sat"];
[array addObject:@"Sun"];
[array addObject:@"Mon"];
[array addObject:@"Tues"];
[array addObject:@"Wed"];
[array addObject:@"Thur"];
[array addObject:@"Fri"];
[array addObject:@"Sat"];}
- (void)viewDidUnload
{
[super viewDidUnload];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return 1;//(interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{
return [array count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell==nil)
{
cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.text = [array objectAtIndex:indexPath.row];
return cell;
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
 if(indexPath.row % 2 == 0){
[cell setBackgroundColor:[UIColor yellowColor]];
}
else
[cell setBackgroundColor:[UIColor redColor]];
}
@end

Step 7

Click on the "Run" button to show the output.

Output in iPhone:

Output-in-iPhone.png

Up Next
    Ebook Download
    View all
    Learn
    View all