2011-07-30 30 views
0

我想弄清楚如何使我的UITableViewController类中的单元格具有像这个问题中的梯度(http://stackoverflow.com/questions/6880416/quick-question-with-uitableviewcell-shadow) 。我看过几种说法使用图像背景,但我宁愿用代码来做。我很难找到如何做到这一点,并感谢任何帮助。我如何使我的UITableViewController中的单元格具有渐变?

回答

1

Matt Gallagher做了一个很好的演练:Adding shadow effects to UITableView using CAGradientLayer

它使用绘制CAGradientLayer作为单元格的背景视图的自定义UIView子类。建立视图的代码trivial-

在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的网站上有一个很好的,简单的示例应用程序。

只是一个nit:细胞在UITableView,而不是控制器。

相关问题