uitableviewcell custom background issue

2019-08-29 09:31发布

问题:

I have a grouped tableview where I have one row in each section. I am setting the backgroundview of the cell to my custom image and I wish the row is rendered this way:

But it renders like this (notice the absence of shadows, it's flat):

Here's the code in cellforRowAtIndexPath:

((UIImageView *)cell.backgroundView).image = [UIImage imageNamed:@"BGImage"];

Where BGImage is the first image in this post, with shadows. What am I missing here?

回答1:

you have to set Your Cell Background using bellow Delegate Method of UITableView:-

- (void)tableView: (UITableView*)tableView
  willDisplayCell: (UITableViewCell*)cell
forRowAtIndexPath: (NSIndexPath*)indexPath
{
            UIImage *img = [UIImage imageNamed:@"yourImage.png"];  // get your background image
            UIColor *backgroundColor = [[UIColor alloc] initWithPatternImage: img];
            cell.backgroundColor = backgroundColor;
            cell.textLabel.backgroundColor = [UIColor clearColor];
            cell.detailTextLabel.backgroundColor = [UIColor clearColor]; 

}

at your issue may be your image Height width and your cell height width not same

I am using above method in my project set with bellow shaded image set in Cell:-

and its look like in project :-



回答2:

  1. Your shadow must be a part of your image.
  2. You have to properly set the height and width.
  3. I'd suggest you to create a custom UITableViewCell.
  4. Add background image to your cell. It'll surely display the shadow.