如何为tableview的每个单元格设置不同的图像

时间:2012-04-28 10:27:18

标签: iphone

如何为表格视图的每个单元格设置不同的图像。我想通过图像名称数组

来做到这一点

4 个答案:

答案 0 :(得分:6)

使用以下代码......

- (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;
}

希望,这会对你有帮助......

答案 1 :(得分:5)

例如:

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

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

答案 2 :(得分:5)

您的解决方案在此处转到此链接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与图像显示如

enter image description here

答案 3 :(得分:-1)

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