自定义表格单元格以显示图像
我打算将图像(通过 URL 字符串)添加到表视图中。但是,照片图像不会显示在表中。下面是我的代码。
- (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;
}
我所有的图像都有不同的尺寸。如何使表格根据图像的大小显示图像?
I am intending to add images (via URL string) into a table view. However, the photo images are not displayed in the table. Below is my code for this.
- (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;
}
All my images are of different sizes. How do I make the table show the images based on their sizes?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
下面的博客文章可以指导您从互联网下载单元格中的图像。
MHLazyTableImages – 高效加载大型表格的图像
HJCache:用于异步图像加载的 iPhone 缓存库和缓存。
当您根据图像询问单元格大小时,我建议您在预定义矩形中显示所有图像,并且所有图像都相同。
虽然您可以根据图像大小更改每个单元格的大小,但这会降低
UITableView
的性能。The below blog posts could guide you for downloading the images in cells from internet.
MHLazyTableImages – Efficiently Load Images for Large Tables
HJCache: iPhone cache library for asynchronous image loading and caching.
As you asked for the cell size based on your images, i would recommend you to show all the images in the predefine rectangle and would be same for all.
Although you could alter the size of each the cell based on the images size but it would decrease the performance of your
UITableView
.