简体   繁体   English

UITableViewCell透明背景(包括imageView / accessoryView)

[英]UITableViewCell transparent background (including imageView/accessoryView)

When I set the UITableViewCells backgroundColor to a semi-transparent color, it looks good, but the color doesn't cover the entire cell. 当我将UITableViewCells backgroundColor设置为半透明颜色时,它看起来不错,但颜色并不覆盖整个单元格。

The area around the imageView and accessoryView are coming up as [UIColor clearColor] ... 周围区域imageViewaccessoryView都上来了作为[UIColor clearColor] ...

替代文字

I've tried explicitly setting the cell.accessoryView.backgroundColor and cell.imageView.backgroundColor to be the same color as the cell's backgroundColor , but it doesn't work. 我已经尝试将cell.accessoryView.backgroundColorcell.imageView.backgroundColor显式设置为与单元格的backgroundColor相同的颜色,但它不起作用。 It puts a tiny box around the icon, but doesn't expand to fill the left edge. 它在图标周围放置一个小盒子,但不会扩展以填充左边缘。 The right edge seems unaffected by this. 右边缘似乎不受此影响。

How can I fix this? 我怎样才能解决这个问题?

EDIT : Here is the raw table cell code: 编辑 :这是原始表格单元格代码:

- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
        cell.opaque = NO;
        cell.textLabel.backgroundColor = [UIColor clearColor];
        cell.backgroundColor = [UIColor colorWithRed:.1 green:.1 blue:.1 alpha:.4];
        cell.textColor = [UIColor whiteColor];
    }

    cell.imageView.image = [icons objectAtIndex:indexPath.row];
    cell.textLabel.text = [items objectAtIndex:indexPath.row];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    return cell;
}

Ben and I figured this out today, here's the summary for the group in case this catches anybody else. Ben和我今天想出了这个,这是该小组的总结,以防其他人抓到。

You have to set the cell background and cell.textLabel.backgroundColor every time cellForRowAtIndexPath is called, not just during the alloc/init phase (ie if the tableView has a dequeue cache miss). 每次调用cellForRowAtIndexPath都必须设置单元格背景和cell.textLabel.backgroundColor ,而不仅仅是在alloc/init阶段(即如果tableView有一个出队缓存未命中)。

So, the code becomes this: 所以,代码变成了这样:

- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
        cell.opaque = NO;        
    }

    // All bgColor configuration moves here
    cell.textLabel.backgroundColor = [UIColor clearColor];
    cell.backgroundColor = [UIColor colorWithRed:.1 green:.1 blue:.1 alpha:.4];
    cell.textColor = [UIColor whiteColor];
    cell.imageView.image = [icons objectAtIndex:indexPath.row];
    cell.textLabel.text = [items objectAtIndex:indexPath.row];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    return cell;
}

Have you looked into setting the background color of the cell's contentView and keeping the cell, accessory, and image views transparent? 您是否考虑过设置单元格contentView的背景颜色并保持单元格,附件和图像视图的透明度?

Also, this article might help show you some alternatives. 此外, 本文可能会帮助您了解一些替代方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM