これが私の cellForRowAtIndexPath: メソッドで、tableView にデータと画像を入力しています。画像のサイズを小さくして、すべて同じサイズにしようとしています。各セル行に 2 つの画像があります。1 つは、すべての行で同じ静的イメージです。この画像いいですね。特定のサイズにサイズを合わせようとしているのは、遅延して読み込まれている画像 ( 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;
}