我打算将图像(通过 URL 字符串)添加到表格 View 中。但是,照片图像不会显示在表格中。下面是我的代码。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectZero];
imageView.tag = kImageValueTag;
[cell.contentView addSubview:imageView];
[imageView release];
}
// Configure the cell...
NSUInteger row = [indexPath row];
Photo *thisPhoto = [self.thisArray objectAtIndex:row];
UIImageView *thisImageView = (UIImageView *)[cell.contentView viewWithTag:kImageValueTag];
NSData * imageData = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString: thisPhoto.imageURL]];
NSLog(@"URL: %@",thisPhoto.imageURL);
thisImageView.image = [UIImage imageWithData:imageData];
return cell;
}
我所有的图片都有不同的尺寸。如何让表格根据图片大小显示图片?
最佳答案
以下博文可以指导您从互联网上下载单元格中的图像。
MHLazyTableImages – Efficiently Load Images for Large Tables
HJCache: iPhone cache library for asynchronous image loading and caching.
当您根据图像要求单元格大小时,我建议您在预定义矩形中显示所有图像,并且所有图像都相同。
尽管您可以根据图像大小更改每个单元格的大小,但这会降低 UITableView
的性能。
关于objective-c - 自定义表格单元格以显示图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6173312/