开发者

Customize table cells to display images

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 dequeueReusableCellWithId开发者_运维知识库entifier: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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜