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

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

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

这是 cellforRowAtIndexPath 中的代码:

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

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

【问题讨论】:

    标签: iphone ios objective-c cocoa-touch uitableview


    【解决方案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】:
    1. 你的影子必须是你形象的一部分。
    2. 您必须正确设置高度和宽度。
    3. 我建议您创建一个自定义 UITableViewCell
    4. 为您的单元格添加背景图像。它肯定会显示阴影。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-02-03
      • 1970-01-01
      • 2011-08-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-21
      • 2013-02-26
      相关资源
      最近更新 更多