UITableViewCell渐变层自动调整大小

发布于 2024-09-29 18:43:46 字数 1610 浏览 0 评论 0原文

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

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

我尝试将图层的框架设置为 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;
}

I'm using the CAGradientLayer method from this answer to set a gradient on my UITableViewCells.

However, for this table, I increased the height of the cells via tableView:heightForRowAtIndexPath:. My gradient layer now does not cover the full height of cell, but instead stops at the original height (see pic).

I tried setting the frame of the layer to the cell bounds in tableView:cellForRowAtIndexPath: but that didn't work either. Is there some way to specify the layer should be autoresized?

Thanks!

Code:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

爱殇璃 2024-10-06 18:43:46

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

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

不做

    gradientLayer.frame = cell.bounds;

Had this problem if your cell height is coded as 55 then you should do the below this will then fill the cell for this table.

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

dont do

    gradientLayer.frame = cell.bounds;
许一世地老天荒 2024-10-06 18:43:46

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

I think you're going the wrong way to customize your table cells. See the http://cocoawithlove.com/2009/04/easy-custom-uitableview-drawing.html.

悲念泪 2024-10-06 18:43:46

我最终使用了 UITableViewDelegate' s 方法:

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

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

I ended up using UITableViewDelegate's method:

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

In it, I check whether the gradient layer has already been added, and if not, I add it.
Not totally happy with it, but it allowed me to move on.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文