gaurav
gaurav

Reputation: 347

How to add lazy image loading to UIimageview

Hello i am using the EGOImageView for lazy image loading.I m using the same code on UITable view. Firstly i am configuring the cell and then with the use of tableviewCellWithReuseIdentifier returning the cell.I am using the code:

This is my tableviewCellWithReuseIdentifier where i am defining the UIImageView with tag:

- (UITableViewCell *)tableviewCellWithReuseIdentifier:(NSString *)identifier 

{

if([identifier isEqualToString:@"UICell"])
{
    UITableViewCell *uiCell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:identifier] autorelease];

    uiCell.textLabel.textAlignment = UITextAlignmentCenter;
    uiCell.textLabel.font = [UIFont systemFontOfSize:16];
    return uiCell;
}

CGRect rect;

rect = CGRectMake(0.0, 0.0, 320.0, 70.0);


UITableViewCell *cell = [[[UITableViewCell alloc] initWithFrame:rect reuseIdentifier:identifier] autorelease];
//  [cell setAccessoryType:UITableViewCellAccessoryDetailDisclosureButton];
cell.selectionStyle =UITableViewCellSelectionStyleNone;

     UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 70.0)];
imageView.tag = BG_Image;
[cell.contentView addSubview:imageView];
[imageView release];
  return cell;

}

AND this the function where i am configuring the cell:

-(void)configureCell:(UITableViewCell *)cell forIndexPath:(NSIndexPath *)indexPath 

{

UIImageView *imageView = (UIImageView *)[cell viewWithTag:BG_Image];
imageView.image = [UIImage imageNamed:@"event_box_bg.png"];

}

this is my EGO image code after initialization i want to know how to use this code and where

EGOImageView *_eventImageView = [[EGOImageView alloc] initWithPlaceholderImage:[UIImage imageNamed:@"placeholder.png"]];

Please help..

Thanks,

Upvotes: 1

Views: 1448

Answers (1)

JeanLuc
JeanLuc

Reputation: 4903

Add an EGOImageView to cell.ContentView in your method tableviewCellWithReuseIdentifier with the code you already posted.

Like this

- (UITableViewCell *)tableviewCellWithReuseIdentifier:(NSString *)identifier 
{
    if([identifier isEqualToString:@"UICell"])
    {
        ...
    }
    CGRect rect = CGRectMake(0.0, 0.0, 320.0, 70.0);
    UITableViewCell *cell = [[[UITableViewCell alloc] initWithFrame:rect reuseIdentifier:identifier] autorelease];
    cell.selectionStyle =UITableViewCellSelectionStyleNone;

    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 70.0)];
    // you can assign the static background image right here   
    imageView.image = [UIImage imageNamed:@"event_box_bg.png"];
    [cell.contentView addSubview:imageView];
    [imageView release];

    // Create an EGOImageView
    EGOImageView * egoImageView = [[EGOImageView alloc] initWithPlaceholderImage:[UIImage imageNamed:@"placeholder.png"]];
    egoImageView.tag = EGO_Image;
    [cell.contentView addSubview:egoImageView];
    [egoImageView release];

    return cell;
}

AND in configureCell

-(void)configureCell:(UITableViewCell *)cell forIndexPath:(NSIndexPath *)indexPath 
{
    EGOImageView *egoImageView = (EGOImageView *)[cell viewWithTag:EGO_Image];
    egoImageView.imageURL = [NSURL URLWithString:@"http://....jpg"];
}

assuming: you run your UITableViewDataSource like that:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"YourTableViewCellID"]
    if (cell == nil)
    {
            cell = [self tableviewCellWithReuseIdentifier:@"YourTableViewCellID"];
    }
    [self dataSource configureCell:cell forIndexPath:indexPath];

    return cell;
}

Upvotes: 2

Related Questions