0

在我的应用程序中,我需要在 UITableView 中显示多个图像,所以我已经在网上搜索了很多将大图像加载到 UITableViewCells 的正确方法,更清楚的是,我将划分我的应用程序执行的过程:

  1. 异步下载图片;
  2. 将它们保存到 NSHomeDirectory();

=> Thins 部分运行良好。

问题是,如何在 UITableViewCell 中显示图像,我已经尝试将 UIImageView 添加到单元格 contentView 但滚动性能有点影响,我在 Apple 指南上进行了搜索,我相信正确的方法是添加 UIImageView到单元格并从 NSHomeDirectory() 加载图像,所以:

自定义 UITableViewCell 并将 UIImageView 的(302x302px)添加到其中的最佳方法是什么?

4

3 回答 3

2

要获得最佳的滚动性能,您必须自己绘制 UITableViewCell 的内容。Tweetie 应用程序(现在是官方 Twitter 应用程序)的作者 Loren Brichter 写了一篇非常著名的博客文章。很遗憾,这篇博文已被删除。 不过,这篇文章可能会对您有所帮助。它解释了快速滚动,它有例子,它有一个来自 Loren Brichter 的演示视频。

基本上,您要做的是继承 UITableViewCell 并覆盖该drawRect:方法。要显示图像,您可以执行以下操作:

- (void)drawRect:(CGRect)rect
{
    [myImage drawAtPoint:CGPointMake(10, 10)];
}

这样可以避免布局很多子视图。

于 2012-10-14T20:31:54.653 回答
2

我也有同样的问题。我正在执行以下操作:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString* CustomCellIdentifier = @"CustomCellIdentifier";

    CustomCell* cell = [tableView dequeueReusableCellWithIdentifier:CustomCellIdentifier];
    if (cell == nil) {
        cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CustomCellIdentifier];
    }

    // add subview to cell
    if (cell.customView == NULL) {
         cell.customView = [[CustomView alloc] initWithFrame:cell.frame];
         [cell.contentView addSubview:cell.customView];
    }

    // bind cell data

    return cell;
}
于 2013-05-24T01:08:30.067 回答
0

首先,您需要为 UITableView 创建一个自定义单元格并继续执行以下几点。

  1. 将每行的高度设置为 302 像素为

    -(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    

    { 返回 302.0; }

  2. 使用以下代码在表格的每个单元格创建 UIImageView -(UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"cell"];

            const NSInteger IMAGE_VIEW_TAG=1001;
    
            UIImageView *imageView;
    
            if(cell==nil)
            {
                    cell=[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"] autorelease];
    
                    cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
    
                    imageView =[[[UIImageView alloc] initWithFrame:CGRectMake(10, 0, 302, 302)] autorelease];
                    imageView.tag=IMAGE_VIEW_TAG;
                    imageView.contentMode=UIViewContentModeScaleAspectFit;
                    [cell.contentView addSubview:imageView];
             }
    
    imageView=(UIImageView*)[cell.contentView viewWithTag:IMAGE_VIEW_TAG];
    [imageView setImage:[UIImage imageNamed:@"image.png"];
    return cell;
    

    }

  3. 设置要显示的行数

     -(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
     {
              return 5;
     }
    
  4. 不要忘记添加 TableView 委托和数据源,UITableViewDelegate 和 UITableViewDataSource

于 2012-10-15T11:55:49.417 回答