uitableviewcell 自定义背景问题

Posted

技术标签:

【中文标题】uitableviewcell 自定义背景问题【英文标题】:uitableviewcell custom background issue 【发布时间】:2013-03-05 06:14:38 【问题描述】:

我有一个分组的表格视图,每个部分都有一行。我将单元格的背景视图设置为我的自定义图像,我希望该行以这种方式呈现:

但是它的渲染是这样的(注意没有阴影,它是平的):

这是 cellforRowAtIndexPath 中的代码:

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

其中 BGImage 是本文中的第一张图片,带有阴影。我在这里错过了什么?

【问题讨论】:

【参考方案1】:

您必须使用以下 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]; 


你的问题可能是你的图片Heightwidth和你的手机heightwidth不一样

我在我的项目集中使用上述方法,在单元格中设置了波纹管阴影图像:-

它在项目中的样子:-

【讨论】:

你是对的!除了在我的情况下,我需要将 cell.backgroundView 设置为包含我的图像的图像视图。现在渲染很好。感谢 willDisplayCell 提示!【参考方案2】:
    你的影子必须是你形象的一部分。 您必须正确设置高度和宽度。 我建议您创建一个自定义 UITableViewCell。 为您的单元格添加背景图像。它肯定会显示阴影。

【讨论】:

以上是关于uitableviewcell 自定义背景问题的主要内容,如果未能解决你的问题,请参考以下文章

请问如何自定义UITableViewCell的背景颜色

tvOS 上 UITableViewCell 的自定义背景

ios自定义UITableviewCell选择的背景颜色没有SelectedBackgroundView

如何在 xamarin ios 中自定义 UITableViewCell 的背景颜色?

具有错误背景颜色的自定义 UITableViewCell

为分组的 UITableViewCell 自定义选择的背景颜色