Toseef
Toseef

Reputation: 151

How can i set a different image for each cell of my tableview

How to set a different image for each cell of my table view. i want to do it through array of image names

Upvotes: 2

Views: 968

Answers (4)

Rahul singh
Rahul singh

Reputation: 1

cell.imageView.image = [UIImage imageNamed:@"image.png"];

Upvotes: -1

Ravi Kumar Karunanithi
Ravi Kumar Karunanithi

Reputation: 2218

Your solution is here go to this link How to add UIImage in the UITableView

- (UITableViewCell *)tableView:(UITableView *)tableView  cellForRowAtIndexPath:(NSIndexPath *)indexPath {
     static NSString *MyIdentifier = @"MyIdentifier";
     MyIdentifier = @"TableView";

     MainView *cell = (MainView *)[tableView  dequeueReusableCellWithIdentifier: MyIdentifier];
     if(cell == nil) {
          [[NSBundle mainBundle] loadNibNamed:@"MainView" owner:self   options:nil];
           cell = tableCell;
        }

  [cell LabelText:[arryList objectAtIndex:indexPath.row]];
 [cell ProductImage:[imagesList objectAtIndex:indexPath.row]];
  return cell;
  }

Uitableview with image shows like

enter image description here

Upvotes: 5

Quirin
Quirin

Reputation: 720

for example:

cell.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"image%i.png", indexPath.row]];

or

cell.imageView.image = (UIImage *)[yourArray objectAtIndex:indexPath.row];

Upvotes: 5

Nitin
Nitin

Reputation: 7461

Use following code...

- (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 *cellImageName = [[self cellImageNames] objectAtIndex:[indexPath row]];//cellImageNames ism image names array where 
UIImage *cellImage = [UIImage cellImageName];
[[cell imageView] setImage:cellIcon];

// Other cell configuration code...

return cell;
}

Hope, this will help you...

Upvotes: 6

Related Questions