如何在ios中重新加载时在动态表格单元格中应用渐变颜色层

如何在ios中重新加载时在动态表格单元格中应用渐变颜色层,ios,objective-c,swift,uitableview,Ios,Objective C,Swift,Uitableview,我想创建一种渐变颜色代码,并将其设置为动态表格视图单元格,该单元格的高度根据数据而变化。 当我要添加到表格单元格的图层时,我使用了图层。我需要准确的表格视图单元格高度后,表格数据加载 -(void)setTableData:(NSMutableArray *)tableData{ tableHeightConstraints.constant = cellHeight * tableData.count; _tableData = tableData; [self

我想创建一种渐变颜色代码,并将其设置为动态表格视图单元格,该单元格的高度根据数据而变化。 当我要添加到表格单元格的图层时,我使用了图层。我需要准确的表格视图单元格高度后,表格数据加载

-(void)setTableData:(NSMutableArray *)tableData{

    tableHeightConstraints.constant  = cellHeight * tableData.count;

    _tableData = tableData;

    [self.tableView reloadData];

    [self layoutIfNeeded];

    CAGradientLayer *bgLayer = [self greyGradient];
    bgLayer.cornerRadius = 5.0f;
    bgLayer.frame = CGRectMake(0, 0, self.contentView.frame.size.width, self.contentView.frame.size.height);
    self.backgroundImageViewTc.image = [self greyImageFromGradient:bgLayer];

    [self layoutIfNeeded];
}


+ (CAGradientLayer*) greyGradient {

    UIColor *colorOne = [UIColor colorWithWhite:0.9 alpha:1.0];
    UIColor *colorTwo = [UIColor colorWithHue:0.625 saturation:0.0 brightness:0.85 alpha:1.0];
    UIColor *colorThree     = [UIColor colorWithHue:0.625 saturation:0.0 brightness:0.7 alpha:1.0];
    UIColor *colorFour = [UIColor colorWithHue:0.625 saturation:0.0 brightness:0.4 alpha:1.0];

    NSArray *colors =  [NSArray arrayWithObjects:(id)colorOne.CGColor, colorTwo.CGColor, colorThree.CGColor, colorFour.CGColor, nil];

    NSNumber *stopOne = [NSNumber numberWithFloat:0.0];
    NSNumber *stopTwo = [NSNumber numberWithFloat:0.02];
    NSNumber *stopThree     = [NSNumber numberWithFloat:0.99];
    NSNumber *stopFour = [NSNumber numberWithFloat:1.0];

    NSArray *locations = [NSArray arrayWithObjects: stopOne, stopTwo, stopThree, stopFour, nil];
    CAGradientLayer *headerLayer = [CAGradientLayer layer];
    headerLayer.colors = colors;
    headerLayer.locations = locations;

    return headerLayer;

}

+ (UIImage *) greyImageFromGradient:(CAGradientLayer*) layer{

    UIGraphicsBeginImageContext(layer.bounds.size);
    [layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return image;
}
此代码无法创建精确大小的图像,因此图像变得模糊。

请尝试此代码:

CGFloat cellHeight = [self tableView:self.tableView heightForRowAtIndexPath:indexPath];
在这里你可以找到你的解决方案,如果有任何问题请告诉我


您想对单元格应用颜色还是设置单元格高度?我想对动态表格单元格应用渐变颜色。问题是如何以这种方式创建渐变色,这样我就可以设置到动态表格视图单元格的背景中。你们能给我看一下你们做的一些代码吗?你们是在创建自定义单元格吗?显示一些您想要的代码或屏幕截图。现在检查我已经完成的源代码。重新加载后我将这些代码放在哪里?我是如何从[self.tableView indexPathsForVisibleRows]中获取indexPath的是您想要的高度。rowatinexpath的高度不可用ok我得到了它willDisplayCell方法会给我精确的动态单元格高度谢谢。
- (void)tableView:(UITableView )tableView willDisplayCell:(UITableViewCell )cell forRowAtIndexPath:(NSIndexPath *)indexPath{

    TVCell mycell = (TVCell )cell;

    CGSize size = CGSizeMake(mycell.cellBkView.frame.size.width, mycell.cellBkView.frame.size.height);

    CAGradientLayer *bgLayer = [BackgroundLayer greyGradient];
    bgLayer.cornerRadius = 5.0f;
    bgLayer.frame = CGRectMake(0, 0, size.width, size.height);
    mycell.cellBkView.backgroundColor = [UIColor colorWithPatternImage:[BackgroundLayer greyImageFromGradient:bgLayer]];
}