UITableViewCell具有自定义渐变背景,另一个渐变为高亮颜色

Ric*_*ich 10 iphone objective-c uitableview

我有一个自定义UITableViewCell与自定义布局.我在的UITableViewDelegate的cellForRowAtIndexPath需要一个渐变的背景,所以:方法,我创建了一个CAGradientLayer并将其添加到细胞与insertSubLayer层:atIndex:(使用索引0).除了两件事之外,这很好用:

最重要的是,当突出显示行时,我无法弄清楚如何更改为不同的渐变颜色.我尝试了几件事,但我对框架不够熟悉,无法让它发挥作用.将代码放入表委托或单元本身的理想位置在哪里?

此外,表格中每个单元格之间有1px的空白区域.我在主视图上有背景颜色,桌面上有背景颜色,单元格上有背景颜色.在UITableView中默认是否有某种填充或间隔?

Ant*_*ony 19

我知道这个帖子已经过时了,但是这里是问题第一部分的解决方案(向单元格的选定状态和非选定状态添加渐变):

-(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 blackColor] CGColor], nil];

    [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];
}
Run Code Online (Sandbox Code Playgroud)

  • 如果你有一个长表,这个方法将继续向同一个单元格添加东西 (4认同)

Rob*_*ens 2

我不确定第一个问题,但我认为您可以像设置 backgroundView 属性一样设置 selectedBackgroundView 属性。单元格之间的空白区域可能是分隔符。你可以像这样改变颜色

tableView.separatorColor = [UIColor redColor];
Run Code Online (Sandbox Code Playgroud)