在 cellForRowAtIndexPath 中手動建立 UIImageView,以圖片實際尺寸設定 frame,避免 Cell 自動縮放導致變形。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
NSData *imageData = ...; // 取得圖片資料
UIImage *image = [UIImage imageWithData:imageData];
if (image) {
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(2, 8, image.size.width, image.size.height)];
imgView.image = image;
[cell.contentView addSubview:imgView];
}
return cell;
}
image.size.width/height 作為 frame 尺寸,保持原始比例。cell.contentView 而非直接加入 cell。UIImageView 屬性,複用時只更新 image 與 frame。