2011-05-30 171 views
1

我打算將圖像(通過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; 
} 

我的所有圖片都是不同的大小。我如何讓表格根據其尺寸顯示圖像?

回答

2

以下博客文章可以指導您從互聯網下載單元格中的圖像。

MHLazyTableImages – Efficiently Load Images for Large Tables

HJCache: iPhone cache library for asynchronous image loading and caching.

至於你問基於您的圖像單元格大小,我建議你,以顯示在預定義矩形的所有圖像和將同所有。

儘管您可以根據圖像大小更改每個單元的大小,但會降低UITableView的性能。

+0

你知道嗎?如果我有2張圖片需要在單個單元格中加載,是否可以實現MHLazyTableImages中的代碼? – Zhen 2011-05-31 02:40:41

相關問題