如何在iOS中的UITableViewCell中制作渐变背景?

use*_*015 12 iphone ipad

我按照下面的教程使用CAGradientLayer在UITableViewCell中制作渐变背景.

http://cocoawithlove.com/2009/08/adding-shadow-effects-to-uitableview.html

除了本教程,本主题还有其他资源吗?

谢谢.

Sus*_*cob 40

总是很棒的Ray Wenderlich做了一个关于改变UITableViewCells的教程并包含了一个渐变.

http://www.raywenderlich.com/2033/core-graphics-101-lines-rectangles-and-gradients

如果你想快速和方式,这里有一些代码:

//include #import <QuartzCore/QuartzCore.h> in the header…

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

 if (cell == nil) {
        cell = [[DayCalendarCellView alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        CAGradientLayer *gradient = [CAGradientLayer layer];
        gradient.frame = cell.bounds;
        gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor whiteColor]CGColor], (id)[[UIColor redColor]CGColor], nil];
        [cell.layer addSublayer:gradient];
    }        
    return cell;
}
Run Code Online (Sandbox Code Playgroud)

你可以改变颜色,但这会给你一个好主意......

祝好运!

  • 谢谢你的工作完美...只需要添加以下行[cell.layer insertSublayer:gradient atIndex:0]; 而不是[cell.layer addSublayer:gradient]; (7认同)
  • 如果您需要UITableViewCell的其他图层/子视图,请小心这种方法,因为您的渐变可以覆盖它们. (2认同)