繁体   English   中英

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

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

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

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

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

希望这个能对您有所帮助...

例如:

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

要么

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

您的解决方案是在这里链接如何在UITableView中添加UIImage

- (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与图像显示像

在此输入图像描述

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM