views:

343

answers:

2

i want to set different image icon for the each cell of tabelview , i dont know how to do this , pls help me.

+2  A: 

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.

Matt Long
A: 
  1. 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...
    @end
    

    In your implementation (.m) file:

    @implementation MyViewController
    @synthesize cellIconNames;
    // Other implementation code...
    @end
    
  2. In your viewDidLoad method, set the cellIconNames property 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]];
    
  3. 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 UIImage object (using cellIconName to specify the image) and set the cell's imageView to this UIImage object:

    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;
}
Steve Harrison
what is listIcon in this code , and where we are using the cellIconNames that we are using as an array.
uttam
@uttam: Whoops! `listIcon` should be `cellIcon` (I've corrected the code now). `cellIconNames` is an `NSArray`, so we use it as an array...
Steve Harrison
what is step 4 , you have given the step-1,2,3 but not the step-4,.and is setCellIconNames is same as cellIconNames.
uttam
Steve Harrison
@uttam: I would strongly recommend you read http://cocoadevcentral.com/d/learn_objectivec/, which gives you a very good introduction to Objective-C/Cocoa concepts, such as accessors and mutators.
Steve Harrison
@uttam: By the way, instead of writing `[self setCellIconNames:[NSArray ar...`, I could have used the new Objective-C 2.0 dot syntax `self.cellIconNames = [NSArray ar...`, but I prefer using the old bracket syntax.
Steve Harrison