objective-c - 自定义表格单元格以显示图像

标签 objective-c cocoa-touch ios uitableview uiimageview

我打算将图像(通过 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/

相关文章:

ios - iOS CollectionView 中的单元格重新排序

ios - 点击一个单元格也会影响其他单元格

ios - 在 Interface Builder 中将 subview 添加到动态项时应用程序崩溃

ios - 如何在两个自动布局约束之间切换?

ios - Xcode - 以 64 位编译应用程序时发生错误

objective-c - 在用相同类型的另一个实例替换目标对象的实例后,如何保持 Cocoa 绑定(bind)正常工作?

objective-c - 相对日期格式,过去日期的输出?

ios - 如何在 SKScene 中呈现 UIViewController?

ios - 为什么 swift 在此处使用结构作为字典键而不是字符串?

iphone - 带有 AFNetworking 和离线模式的 SDURLCache 不起作用