如何让我的UITableViewController中的单元格具有渐变?

时间:2011-07-30 03:26:08

标签: uitableview

我正在试图弄清楚如何使我的UITableViewController类中的单元格具有类似于此问题中的渐变(http://stackoverflow.com/questions/6880416/quick-question-with-uitableviewcell-shadow )。我已经看到了几个使用图像背景的事情,但我宁愿用代码来做。我无法找到如何做到这一点,并感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

马特·加拉格尔(Matt Gallagher)对这一段时间进行了很好的演练:Adding shadow effects to UITableView using CAGradientLayer

它使用自定义UIView子类绘制CAGradientLayer作为单元格的背景视图。设置视图的代码很简单 -

在GradientView UIView子类中:

CAGradientLayer *gradientLayer = (CAGradientLayer *)self.layer;
gradientLayer.colors = [NSArray arrayWithObjects:
         (id)[UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:1.0].CGColor,
         (id)[UIColor colorWithRed:0.85 green:0.85 blue:0.85 alpha:1.0].CGColor, nil];

self.backgroundColor = [UIColor clearColor];

在UITableViewController的 cellForRowAtIndexPath:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [{ Set up your cell}];
    cell.backgroundView = [[[GradientView alloc] init] autorelease];
}

当然,有一些并发症,但在Matt的网站上有一个很好的,简单的示例应用程序。

只是一个单词:单元格在UITableView,而不是控制器。

相关问题