3
votes

I try to add a background image to my UITableViewCell. This is the code i am using:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"TableCell";

    // Dequeue or create a cell of the appropriate type.
    TableCell *cell = (TableCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    cell.backgroundView =  [[UIImageView alloc] initWithImage:[ [UIImage imageNamed:@"cellbg.jpeg"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ];
    cell.selectedBackgroundView =  [[UIImageView alloc] initWithImage:[ [UIImage imageNamed:@"cellbg.jpeg"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ];

    if (cell == nil) {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"TableCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }

    [cell.preparationLabel setFont:[UIFont fontWithName:@"Bold" size:20]];

    // Configure the cell.
    if (btnselected==1)
    {
        cell.preparationLabel.text = [recipe.ingredients objectAtIndex:indexPath.row];
    }
    else
    {
     cell.preparationLabel.text = [recipe.preparation objectAtIndex:indexPath.row];
    }
    return cell;
}

My problem is ,While running the app and selecting the tableview ,it first shows the table cell with default white background and after some time cell backgrounds will get changed to the desire image...I dont know why it is taking a long time to load the table view with my desired cell background

4
refer stackoverflow.com/a/15353155/1713478 answer it will help youPratik

4 Answers

6
votes

The slow loading is because you are adding the UIImageView to the cell every time instead of only when creating a new one.

Change your code as follows:

   // Dequeue or create a cell of the appropriate type.
    TableCell *cell = (TableCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"TableCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];

        cell.backgroundView =  [[UIImageView alloc] initWithImage:[ [UIImage imageNamed:@"cellbg.jpeg"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ];
        cell.selectedBackgroundView =  [[UIImageView alloc] initWithImage:[ [UIImage imageNamed:@"cellbg.jpeg"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ];
    }
3
votes

You need to move

cell.backgroundView =  [[UIImageView alloc] initWithImage:[ [UIImage imageNamed:@"cellbg.jpeg"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ];
cell.selectedBackgroundView =  [[UIImageView alloc] initWithImage:[ [UIImage imageNamed:@"cellbg.jpeg"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ];

lines to after the if (cell == nil)... condition. Otherwise, the cells get new background only after being recycled, while the newly created ones remain without the background.

2
votes
if (cell == nil) 
{
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"TableCell" owner:self options:nil];

        UIImageView *imgView1 = [[UIImageView alloc]init];..continue...
        UIImageView *imgView2 = [[UIImageView alloc]init];..continue...

        [cell.backgroundView addSubView:imgView1];
        [cell.selectedBackgroundView addSubView:imgView2];
        cell = [nib objectAtIndex:0];
}
0
votes
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {

    UIImageView *ivwCellHighlighted = [[UIImageView alloc]initWithImage:[UIImage imageNamed:CELL_SELECTEDIMAGE]];
    [ivwCellHighlighted setFrame:CGRectMake(1, 1, 320, 47)];
    [cell setSelectedBackgroundView:ivwCellHighlighted];
    [ivwCellHighlighted release];
    ivwCellHighlighted=nil;


}