0

我正在尝试向自定义单元格的背景添加渐变,当我使用 addSublayer 显示渐变时,它会显示在其余内容上,而当我使用 insertSublayer: atIndex: 0 时,它不会出现。此单元格是自定义单元格,我该怎么办?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:        (NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"ScoreCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    CAGradientLayer *gradient = [CAGradientLayer layer];
    gradient.frame = cell.bounds;
    gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor yellowColor]CGColor], (id)[[UIColor whiteColor]CGColor], nil];
    [cell.layer insertSublayer:gradient atIndex:1];
    return cell;
}
4

2 回答 2

2

插入以下代码。请记住,子层是从后到前列出的:

cell.backgroundColor = [UIColor clearColor];
[cell.layer insertSublayer:gradient atIndex:0];
于 2014-07-28T21:33:38.957 回答
-1

UITableViewCell 的默认背景颜色为白色。将其设置为清除,您应该能够在索引 0 处插入渐变并显示。

于 2014-07-28T21:21:16.360 回答