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"]];
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
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With