i want to set different image icon for the each cell of tabelview , i dont know how to do this , pls help me.
You can create a custom cell with a UIImageView in it, but the simplest way is to set the built in image view of the default UITableViewCell in your -cellForRowAtIndexPath table view delegate. Something like this:
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithFrame:CGRectZero];
//... other cell initializations here
}
[[cell imageView] setImage:image];
Where image is a UIImage that you created by loading from a URL or from the local application bundle.
Create a property to store an array of different image names.
In your header (
.h) file:@interface MyViewController : UITableViewController { NSArray *cellIconNames; // Other instance variables... } @property (nonatomic, retain) NSArray *cellIconNames; // Other properties & method declarations... @endIn your implementation (
.m) file:@implementation MyViewController @synthesize cellIconNames; // Other implementation code... @endIn your
viewDidLoadmethod, set thecellIconNamesproperty to an array containing the different image names (in the order they want to appear):[self setCellIconNames:[NSArray arrayWithObjects:@"Lake.png", @"Tree.png", @"Water.png", @"Sky.png", @"Cat.png", nil]];In your
tableView:cellForRowAtIndexPath:table view data source method, get the image name that corresponds with the cell's row:NSString *cellIconName = [[self cellIconNames] objectAtIndex:[indexPath row]];Then create a
UIImageobject (usingcellIconNameto specify the image) and set the cell'simageViewto thisUIImageobject:UIImage *cellIcon = [UIImage imageNamed:cellIconName]; [[cell imageView] setImage:cellIcon];
After step 3, your tableView:cellForRowAtIndexPath: method would look something like this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
/* Initialise the cell */
static NSString *CellIdentifier = @"MyTableViewCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
/* Configure the cell */
NSString *cellIconName = [[self cellIconNames] objectAtIndex:[indexPath row]];
UIImage *cellIcon = [UIImage imageNamed:cellIconName];
[[cell imageView] setImage:cellIcon];
// Other cell configuration code...
return cell;
}