【问题标题】:how to apply gradient color layer in dynamic table cell at reload in ios如何在 ios 重新加载时在动态表格单元格中应用渐变颜色层
【发布时间】:2016-06-20 10:49:17
【问题描述】:

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

-(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;
}

此代码无法创建精确尺寸的图像,因此变得模糊。

【问题讨论】:

  • 你想对单元格应用颜色还是要设置单元格的高度?
  • 我想对动态表格单元格应用渐变色。问题是如何以这种方式创建渐变颜色,以便我可以设置为动态表格视图单元格的背景。
  • 你能告诉我一些你做的代码吗?
  • 您在创建自定义单元格吗?显示一些你想要的代码或屏幕截图。
  • 现在检查我已经完成的源代码。

标签: ios objective-c swift uitableview


【解决方案1】:

在这里你可以找到你的解决方案,如果有任何问题请告诉我

- (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]];
}

【讨论】:

  • 好的,我知道了 willDisplayCell 方法会给我精确的动态单元格高度,谢谢。
【解决方案2】:

试试这个代码:

CGFloat cellHeight = [self tableView:self.tableView heightForRowAtIndexPath:indexPath];

【讨论】:

  • 重新加载后我把这段代码放在哪里?以及我如何从 [self.tableView indexPathsForVisibleRows] 获得 indexPath
  • 是的,你想要的高度。
  • heightForRowAtIndexPath 不可用
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多