简体   繁体   English

uitableviewcell自定义背景问题

[英]uitableviewcell custom background issue

I have a grouped tableview where I have one row in each section. 我有一个分组的tableview,在每个节中都有一行。 I am setting the backgroundview of the cell to my custom image and I wish the row is rendered this way: 我将单元格的backgroundview设置为我的自定义图像,并希望以这种方式呈现该行:

在此处输入图片说明

But it renders like this (notice the absence of shadows, it's flat): 但是它是这样渲染的(注意没有阴影,它是平坦的):

在此处输入图片说明

Here's the code in cellforRowAtIndexPath: 这是cellforRowAtIndexPath中的代码:

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

Where BGImage is the first image in this post, with shadows. 其中BGImage是此帖子中的第一张图像,带有阴影。 What am I missing here? 我在这里想念什么?

you have to set Your Cell Background using bellow Delegate Method of UITableView:- 您必须使用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 在您的问题可能是您的图像Height width和您的单元格height width不一样

I am using above method in my project set with bellow shaded image set in Cell:- 我在项目集中使用上述方法,并在Cell中设置了波纹管阴影图像集:-

在此处输入图片说明

and its look like in project :- 它看起来像项目中的:-

在此处输入图片说明

  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 . 我建议您创建一个自定义UITableViewCell
  4. Add background image to your cell. 将背景图像添加到您的单元格。 It'll surely display the shadow. 它一定会显示阴影。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM