这是我的 cellForRowAtIndexPath: 方法,我用一些数据和图像填充 tableView。我正在尝试缩小图像的大小,使它们的大小都相同。每个单元格行都有两个图像。一个是所有行都相同的静态图像。这张图不错。这是我试图调整到某个尺寸的延迟加载的图像(cell.imageView.image)。
谢谢你的帮助
这是我的代码:
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:CellIdentifier];
UILabel *labelTitle = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
labelTitle.tag = 100;
labelTitle.textAlignment = UITextAlignmentLeft;
labelTitle.numberOfLines = 1;
labelTitle.font = [UIFont fontWithName:@"DIN-Bold" size:16];
[cell.contentView addSubview:labelTitle];
UILabel *labelDetail = [[UILabel alloc] initWithFrame:CGRectMake(0, 30, 320, 44)];
labelDetail.tag = 101;
labelDetail.textAlignment = UITextAlignmentLeft;
labelDetail.numberOfLines = 1;
labelDetail.font = [UIFont fontWithName:@"DIN-Regular" size:14];
[cell.contentView addSubview:labelDetail];
// cell image
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(250, 20, 36, 36)];
imgView.image = [UIImage imageNamed:@"someImage.png"];
imgView.contentMode = UIViewContentModeCenter;
imgView.alpha = 0.1;
[cell addSubview:imgView];
CGSize itemSize = CGSizeMake(40, 40);
UIGraphicsBeginImageContext(itemSize);
CGRect imageRect = CGRectMake(0.0, 0.0, itemSize.width, itemSize.height);
[cell.imageView.image drawInRect:imageRect];
cell.imageView.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
NSUInteger row = indexPath.row;
UILabel *labelTitle1 = (UILabel*)[cell viewWithTag:100];
labelTitle1.text = (NSString *)[[publicDataArray objectAtIndex:indexPath.row] objectForKey:@"title"];
UILabel *labelDetail1 = (UILabel*)[cell viewWithTag:101];
labelDetail1.text = (NSString *)[[publicDataArray objectAtIndex:indexPath.row] objectForKey:@"short_description"];
// set the image url & image caching
NSString *imageUrlString = [NSString stringWithFormat:@"%@", [[publicDataArray objectAtIndex:indexPath.row] objectForKey:@"image"]];
UIImage *cachedImage = [self.imageCache objectForKey:imageUrlString];
if (cachedImage)
{
cell.imageView.image = cachedImage;
}
else
{
cell.imageView.image = [UIImage imageNamed:@"rest_small.png"]; // initialize the image with placeholder image
// download in the image in the background
[self.imageDownloadingQueue addOperationWithBlock:^{
NSURL *imageUrl = [NSURL URLWithString:imageUrlString];
NSData *imageData = [NSData dataWithContentsOfURL:imageUrl];
UIImage *image = nil;
if (imageData)
image = [UIImage imageWithData:imageData];
if (image)
{
[self.imageCache setObject:image forKey:imageUrlString]; // add the image to your cache
// finally, update the user interface in the main queue
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
// make sure the cell is still visible
UITableViewCell *updateCell = [tableView cellForRowAtIndexPath:indexPath];
if (updateCell)
cell.imageView.image = image;
}];
}
}];
}
return cell;
}