2012-07-05 76 views
1

我需要自定义部分部分,其中包含分组的风格的UITableView的几个单元格。实际上,我想为单元格的每个部分设置渐变颜色背景。每个部分的单元格数目可能会更改。如何为分组单元格的UITableView部分绘制渐变色背景

我发现很多解决方案为每个单元定制一个背景,但不定制一个单元格区域。

为为例,下面的代码设置背景颜色每每个部分的单元:

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath 
{  

[cell setBackgroundColor:[UIColor clearColor]]; 

CAGradientLayer *grad = [CAGradientLayer layer]; 
grad.frame = cell.bounds; 
grad.colors = [NSArray arrayWithObjects:(id)[[UIColor whiteColor] CGColor], (id)[[UIColor greenColor] CGColor], nil]; 
grad.cornerRadius = 5.0; 

[cell setBackgroundView:[[UIView alloc] init]]; 
[cell.backgroundView.layer insertSublayer:grad atIndex:0]; 


CAGradientLayer *selectedGrad = [CAGradientLayer layer]; 
selectedGrad.frame = cell.bounds; 
selectedGrad.colors = [NSArray arrayWithObjects:(id)[[UIColor blackColor] CGColor], (id)[[UIColor whiteColor] CGColor], nil]; 

[cell setSelectedBackgroundView:[[UIView alloc] init]]; 
[cell.selectedBackgroundView.layer insertSublayer:selectedGrad atIndex:0]; 

} 

这不是我需要实现,我必须设置的线性梯度颜色所有单元为每个部分。

如果有谁面临着同样的问题,并发现了一个解决方案,请让我们知道

非常感谢

回答

-1

地址:

#import <QuartzCore/QuartzCore.h> 

,改变你的代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{  
    if (indexPath.row % 2 == 0) { 
     UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 85)]; 
     CAGradientLayer *gradient = [CAGradientLayer layer]; 
     gradient.frame = view.bounds; 
     gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor colorWithRed:160/255.f green:208/255.f blue:222/255.f alpha:1] CGColor], (id)[[UIColor whiteColor] CGColor], nil]; 
     [cell.layer insertSublayer:gradient atIndex:0]; 
    } else { 
     UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 85)]; 
     CAGradientLayer *gradient = [CAGradientLayer layer]; 
     gradient.frame = view.bounds; 
     gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor whiteColor] CGColor], (id)[[UIColor colorWithRed:160/255.f green:208/255.f blue:222/255.f alpha:1] CGColor], nil]; 
     [cell.layer insertSublayer:gradient atIndex:0]; 
    } 
} 
相关问题