2013-03-06 76 views
1

我有一个自定义tableview,它将被动态对象填充以形成一个列表。我试图在我的单元格中设置图像半顶上的渐变叠加层。IOS渐变叠加在滚动后变成纯色

我在interface.builder中设置了我的布局。

下面是我的问题: - 我有问题,我的渐变叠加看起来每次都会重新绘制,导致它在向上/向下滚动后变成纯色叠加。任何人有任何想法如何克服这一点?

谢谢!


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    CustomTableViewCell *cell = (CustomTableViewCell*)[tableView dequeueReusableCellWithIdentifier:@"TableCellId"]; 
    if(cell == nil){ 
     cell = [[CustomTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"TableCellId"]; 
    } 

    UIImageView *item_image = (UIImageView *)[cell viewWithTag:@"cell_image"]; 
    NSString *url_image = [NSString stringWithFormat:@"%@%@", URL_SERVER_HOST, item_pic]; 

    [item_image setImageWithURL:[NSURL URLWithString:url_image]]; 

    //Currently get called when new cell is loaded 
    UIView *overlay = (UIView *)[cell viewWithTag:@"cell_overlay"]; 
    CAGradientLayer *gradient = [CAGradientLayer layer]; 
    gradient.frame = overlay.bounds; 
    gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor clearColor] CGColor], (id)[[UIColor blackColor] CGColor], nil]; 
    [overlay.layer insertSublayer:gradient atIndex:0]; 

    UILabel * item_name = (UILabel *)[cell viewWithTag:@"cell_name"]; 
    item_name.text = @"test text"; 

    return cell; 
} 

回答

2

将相关的代码为if (cell == nil)梯度,所以当电池被重用,也不会添加渐变层再次

+0

我试过,但不知何故细胞是永远为零,在病因移动它,如果你使用故事板或注册笔尖的tableview创建单元格梯度永远不会被调用 – bruce 2013-03-06 13:45:09

+3

,您可以添加单元类中的-awakeFromNib方法中的渐变图层。否则,您可以在-initWithStyle:reuseIdentifier中添加渐变图层。 – Oscar 2013-03-07 02:51:55

1

我只是把我的叠加子层为nil应用梯度前。只要下面写前行[overlay.layer insertSublayer:gradient atIndex:0];

overlay.layer.sublayers = nil;