How to Set Image on Selected Table Cell 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 set the image on the selected table cell 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
{cell.selectedBackgroundView = [[[UIImageView alloc] init] autorelease];
   UIImage *img  = [UIImage imageNamed:@"toyota_venza.jpg"];
    ((UIImageView *)cell.selectedBackgroundView).image = img;}
@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