将子视图添加到UITableViewCell contentView的正确方法

时间:2012-10-14 20:12:45

标签: iphone xcode uitableview uiimageview

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

  1. 异步下载图片;
  2. 将它们保存到NSHomeDirectory();
  3. =>这部分工作正常。

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

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

3 个答案:

答案 0 :(得分:2)

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

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

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

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

答案 1 :(得分: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;
}

答案 2 :(得分: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