簡體   English   中英

TableView單元格中的自定義ImageView

[英]Custom imageview in tableview cell

此代碼有什么問題? 圖像不顯示。 感謝任何幫助...逐步執行代碼,我可以看到所有變量均已正確更新。 該代碼執行沒有任何錯誤。 雖然看不到圖像。

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

我才找到答案。 問題出在文件名路徑上。 我所有的圖像文件都存儲在一個捆綁包中。 因此,即使正確引用了圖像文件名,也需要添加路徑。

通過這種方式添加路徑可以解決此問題。

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

這個答案也幫助了我。 UITableViewCell具有不同尺寸的圖像

由於您使用的是默認單元格(即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