如何在ios中以编程方式在UITableviewcells中添加图像

嗨,我需要以编程方式在UITableViewCells添加不同的图像。 我怎么做,我正在尝试一些code.But图像不显示在UITableViewCells 。 这是我的代码如下。 请帮助我任何人。 提前致谢。

 -(void)viewDidLoad { arrImages=[[NSMutableArray alloc]initWithObjects:@"image.png",@"image2.png",@"image3.png",@"image4.png",@"image5.png",@"image6.png",@"image7.png",@"image8.png" nil]; } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *identifier=@"cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"identifier"]; } imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 48, 48)]; cell.imageView.image = [UIImage imageNamed:[arrImages objectAtIndex:indexPath.row]]; return cell; } 

在.h文件中创build一个NSArray属性

 @property NSArray *imgArray; 

在你的.m文件中进行合成

 @synthesize imgArray; 

写在你的viewDidLoad

 - (void)viewDidLoad { [super viewDidLoad]; // Initialize images... imgArray = [NSArray arrayWithObjects:@"yourFirstImageName.png", @"yourSecondImageName.png", @"yourThirdImageName.png",....., @"yourSeventhImageName.png", nil]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *simpleTableIdentifier = @"SimpleTableItem"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier]; UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 48, 48)]; // your cell's height should be greater than 48 for this. imgView.tag = 1; [cell.contentView addSubview:imgView]; imgView = nil; UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(x, y, width, height)]; lbl.backgroundColor = [UIColor clearColor]; [lbl setTag:2]; [cell.contentView addSubview:lbl]; lblDisch = nil; } UIImageView *_imgView = (UIImageView *)[cell.contentView viewWithTag:1]; _imgView.image = [UIImage imageNamed:[imgArray objectAtIndex:indexPath.row]]; UILabel *_lbl = (UILabel *)[cell.contentView viewWithTag:2]; _lbl.text = [arrFont objectAtIndex:indexPath.row]; // arrFont is your array return cell; } 

希望这会帮助你。

这是你可能想要的:

 - (void)viewDidLoad { self.arrImages = [[NSMutableArray alloc] initWithObjects:@"image.png",@"image2.png",@"image3.png",@"image4.png",@"image5.png",@"image6.png",@"image7.png",@"image8.png" nil]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString* CellIdentifier = @"Cell"; UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] } cell.imageView.image = [UIImage imageNamed:[self.arrImages objectAtIndex:indexPath.row]]; return cell; } 

PS :下次请花点时间格式化源代码,解释什么是错的,你尝试了什么,你想完成什么。 谢谢

在.h文件中创build一个NSMutableArray的@property。

 NSMutableArray *data; 

写这个代码你的.m文件。

 - (void)viewDidLoad { data = [[NSMutableArray alloc]initWithObjects:@"1.jpg",@"2.jpg",@"3.jpg",@"4.jpg",@"5.jpg",@"6.jpg",@"7.jpg",@"8.jpg",@"9.jpg",@"10.jpg",@"11.jpg",@"12.jpg",@"13.jpg",nil]; [super viewDidLoad]; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [data count]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *cellIdentifier = @"cellID"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; } cell.imageView.image = [UIImage imageNamed:[data objectAtIndex:indexPath.row]]; return cell; }