我需要以编程方式在 UITableViewCells
中添加不同的图像。我怎样才能做到这一点。我正在尝试一些代码。但图像未显示在 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;
}
最佳答案
这可能是您想要的:
- (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;
}
P.S.:下次,请努力格式化源代码,解释哪里出了问题,你尝试了什么,你想完成什么。谢谢
关于ios - 如何在 iOS 中以编程方式在 UITableViewCells 中添加图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22348994/