简体   繁体   English

TableView单元格中的自定义ImageView

[英]Custom imageview in tableview cell

What is wrong with this code? 此代码有什么问题? The image is not displayed. 图像不显示。 Appreciate any help... Stepping through the code, I can see that all the variables are updated correctly. 感谢任何帮助...逐步执行代码,我可以看到所有变量均已正确更新。 The code executes without any errors. 该代码执行没有任何错误。 The image is not seen though. 虽然看不到图像。

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

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ModuleViewCellId];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ModuleViewCellId] autorelease];
}
NSUInteger row = [indexPath row];
NSDictionary *step = [self.module.steps objectAtIndex:row];
cell.textLabel.text = [step objectForKey:kStepTitleKey];//;
cell.textLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:14.0];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.backgroundColor = [UIColor clearColor];
cell.indentationWidth = 50;
cell.indentationLevel = 1;

//2/6 shp - add icons to the table view
NSString *imagename = [step objectForKey:kStepIconKey];
NSLog(@"%@",imagename);

UIImageView *image;

image = [[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 57, 57)] autorelease];
image.tag = 1000 + row;
image.contentMode = UIViewContentModeScaleAspectFit;
image.hidden = FALSE;


[image setImage: [UIImage imageNamed:imagename]];
[cell.contentView addSubview:image];


return cell;
}

I just found the answer. 我才找到答案。 The problem was with the path to the file names. 问题出在文件名路径上。 All my image files are stored in a bundle. 我所有的图像文件都存储在一个捆绑包中。 So even though the image file names are correctly referenced, the path needed to be appended. 因此,即使正确引用了图像文件名,也需要添加路径。

Adding the path this way fixed the problem. 通过这种方式添加路径可以解决此问题。

    NSString *imagename = [NSString stringWithFormat: @"%@%@", @"Assets.bundle/assets_dir/",[step objectForKey:kStepIconKey]];

This answer also helped me. 这个答案也帮助了我。 UITableViewCell with images in different dimensions UITableViewCell具有不同尺寸的图像

Since you are using default cell ie uitableViewCell, make use of imageview property of the cell. 由于您使用的是默认单元格(即uitableViewCell),因此请使用该单元格的imageview属性。

UITableViewCell *cell =[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
cell.imageView.image = [UIImage imageNamed:@"ImageName"];
cell.imageView.contentMode = UIViewContentModeScaleAspectFit;

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

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