【问题标题】:UITableViewCell gradient layer auto resizeUITableViewCell 渐变层自动调整大小
【发布时间】:2010-10-27 15:37:36
【问题描述】:

我正在使用 answer 中的 CAGradientLayer 方法在我的 UITableViewCells 上设置渐变。

但是,对于这个表格,我通过 tableView:heightForRowAtIndexPath: 增加了单元格的高度。我的渐变层现在没有覆盖单元格的整个高度,而是停在原始高度(参见pic)。

我尝试将图层的框架设置为 tableView:cellForRowAtIndexPath: 中的单元格边界:但这也不起作用。有什么方法可以指定图层应该自动调整大小吗?

谢谢!

代码:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 55;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{    
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        CAGradientLayer *gradient = [CAGradientLayer layer];
        gradient.colors = [NSArray arrayWithObjects:
                           (id)[[UIColor purpleColor] CGColor],
                           (id)[[UIColor redColor] CGColor],
                           nil];
        [cell.layer insertSublayer:gradient atIndex:0];
    }
    cell.layer.borderWidth = 1.0;
    CALayer* gradientLayer = [cell.layer.sublayers objectAtIndex:0];
    DebugLog(@"cell bounds: %@", NSStringFromCGRect(cell.bounds));
    gradientLayer.frame = cell.bounds;

    // Configure the cell...
    return cell;
}

【问题讨论】:

  • 请从 cellForRowAtIndexPath 和 heightForRowAtIndexPath 发布您的代码。

标签: iphone objective-c uitableview gradient


【解决方案1】:

如果您的单元格高度编码为 55,则出现此问题,那么您应该执行以下操作,然后填充此表格的单元格。

    CAGradientLayer *gradientLayer = [CAGradientLayer layer];
    gradientLayer.frame = CGRectMake(0, 0, 320, 55);

别这样

    gradientLayer.frame = cell.bounds;

【讨论】:

  • @Alex McPherson,有什么具体原因不应该将单元格绑定到渐变层的框架吗?
【解决方案2】:

我认为您在自定义表格单元格时采用了错误的方式。请参阅http://cocoawithlove.com/2009/04/easy-custom-uitableview-drawing.html

【讨论】:

  • 是的,从长远来看,也许这是一种更好的方法,即当您有艺术家处理这些图像时。现在我只想用代码轻松地调整外观,而不必处理图像和绘图程序。
【解决方案3】:

我最终使用了UITableViewDelegate的方法:

- (void)tableView:(UITableView *)tableView
  willDisplayCell:(UITableViewCell *)cell
forRowAtIndexPath:(NSIndexPath *)indexPath;

在里面我检查渐变层是否已经添加,如果没有,我添加它。
对它并不完全满意,但它让我继续前进。

【讨论】:

    猜你喜欢
    • 2021-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-02
    • 1970-01-01
    相关资源
    最近更新 更多