如何将斜角添加到UITableViewCell

NSA*_*exC 6 iphone cocoa-touch objective-c uitableview

我需要在我的UITableViewCell上添加斜角(顶部为白线,底部为暗线).我已经添加了CAGradientLayer作为我的cell.layer的子视图.不幸的是我不能使用UIImageView作为我的单元格的背景,所以这需要在代码中完成.有什么建议?谢谢!

这是我现在为我的手机背景编写的代码.

CAGradientLayer *gradientLayer = [CAGradientLayer layer];  
gradientLayer.frame = cell.frame;  
gradientLayer.colors = [NSArray arrayWithObjects:
    (id)[[UIColor colorWithRed:0.988 green:0.988 blue:0.988 alpha:1.0] CGColor],
    (id)[[UIColor colorWithRed:0.9294 green:0.9294 blue:0.949 alpha:1.0] CGColor],
    nil];  
[cell.layer insertSublayer:gradientLayer atIndex:0];
Run Code Online (Sandbox Code Playgroud)

这看起来不错,但我想在底部有一条1像素的暗线,在顶部有一条1像素的白线来完成外观.

Pey*_*loW 6

您可以,也可能应该使用a UIImageView作为单元格背景.你目前正在做的事实上是非常错误的.

一个UITableViewCell包括在hiarchy不少意见,重要的是要知道你应该在你的意见挂钩.此层次结构如下:

  • UITableViewCell - 切勿触摸此
    • backgroundView - 替换为所有行设置自定义背景.
    • selectedBackgroundView - 替换为选定/突出显示的行设置自定义背景.
    • contentView - 不要设置它,但随意添加任意数量的子视图.
      • titleLabel
      • detailTitleLabel
      • imageView
      • 你自己的看法
    • accessoryView

从一开始就不明显如何更换backgroundViewselectedBackgroundView.UITableView-[tableView:cellForRowWithIndexPath:]数据源方法返回单元格后,它本身将自动设置它们.意味着您设置的任何内容都将被覆盖.

诀窍是实现-[tableView:willDisplayCell:forRowAtIndexPath:]委托方法,并在此处设置自定义背景.

所有这些都在表视图编程指南中得到了很好的解释.