tableview单元格中的自定义imageview

时间:2013-02-06 05:09:54

标签: ios uitableview uiimageview

这段代码有什么问题?图像不显示。感谢任何帮助...... 单步执行代码,我可以看到所有变量都已正确更新。代码执行没有任何错误。但是看不到图像。

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

2 个答案:

答案 0 :(得分:0)

我刚刚找到答案。问题出在文件名的路径上。我的所有图像文件都存储在一个包中。因此,即使正确引用了图像文件名,也需要追加路径。

以这种方式添加路径可以解决问题。

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

这个答案也对我有所帮助。 UITableViewCell with images in different dimensions

答案 1 :(得分:0)

由于您使用的是默认单元格,即uitableViewCell,请使用单元格的imageview属性。

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