2013-02-06 103 views
0

这段代码有什么问题?图像不显示。欣赏任何帮助... 单步执行代码,我可以看到所有变量都已正确更新。代码执行时没有任何错误。虽然没有看到图像。在tableview单元中自定义imageview

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

回答

0

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

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

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

此答案也帮助了我。 UITableViewCell with images in different dimensions

0

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

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