简体   繁体   中英

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

Since you are using default cell ie uitableViewCell, make use of imageview property of the cell.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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