Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set UITableViewCell background image with different heights?

I have a UITableView and would like to apply a background image to all cells. My height for each cell is variable. How should I go about creating the background image?

cell.contentView.backgroundColor = [UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"background.png"]];
like image 384
Sheehan Alam Avatar asked Jul 20 '10 01:07

Sheehan Alam


2 Answers

One idea is to place a UIImage with a stretchable image on it. That way, it doesn't matter what the row height is, the image can stretch to match.

You can also do something similar to what Matt has done here with a gradient layer

like image 108
iwasrobbed Avatar answered Oct 13 '22 23:10

iwasrobbed


In cellForRowAtIndexPath method you can give cell background color

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


{
    static NSString *cellIdentifier = @"Cell";
    UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    if (cell == nil)
    {    
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
// for cell background color
  cell.backgroundView =[[UIImageView alloc] initWithImage:[ [UIImage imageNamed:@"tab2.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ];

// for selection color      
  cell.selectedBackgroundView =[[UIImageView alloc] initWithImage:[ [UIImage imageNamed:@"selected.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ];
    return cell;
}
like image 2
Swift-Master Avatar answered Oct 13 '22 23:10

Swift-Master