How to set UITableViewCell background image with different heights?
I have a UITableView and would like to a开发者_Python百科pply 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;
}
精彩评论