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像素的白线来完成外观.
您可以,也可能应该使用a UIImageView
作为单元格背景.你目前正在做的事实上是非常错误的.
一个UITableViewCell
包括在hiarchy不少意见,重要的是要知道你应该在你的意见挂钩.此层次结构如下:
UITableViewCell
- 切勿触摸此
backgroundView
- 替换为所有行设置自定义背景.selectedBackgroundView
- 替换为选定/突出显示的行设置自定义背景.contentView
- 不要设置它,但随意添加任意数量的子视图.
titleLabel
detailTitleLabel
imageView
accessoryView
从一开始就不明显如何更换backgroundView
和selectedBackgroundView
.UITableView
从-[tableView:cellForRowWithIndexPath:]
数据源方法返回单元格后,它本身将自动设置它们.意味着您设置的任何内容都将被覆盖.
诀窍是实现-[tableView:willDisplayCell:forRowAtIndexPath:]
委托方法,并在此处设置自定义背景.
所有这些都在表视图编程指南中得到了很好的解释.