【问题标题】:UITableViewCell transparent background (including imageView/accessoryView)UITableViewCell 透明背景(包括imageView/accessoryView)
【发布时间】:2009-10-01 04:55:54
【问题描述】:

当我将 UITableViewCells backgroundColor 设置为半透明颜色时,看起来不错,但颜色并没有覆盖整个单元格。

imageViewaccessoryView 周围的区域将变为 [UIColor clearColor]...

我已尝试将cell.accessoryView.backgroundColorcell.imageView.backgroundColor 显式设置为与单元格的backgroundColor 相同的颜色,但它不起作用。它在图标周围放置了一个小框,但不会扩展以填充左边缘。右边缘似乎不受此影响。

我该如何解决这个问题?

编辑:这是原始表格单元格代码:

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

【问题讨论】:

    标签: iphone iphone-sdk-3.0 uitableview


    【解决方案1】:

    Ben 和我今天想通了,这里是小组的摘要,以防其他人发现。

    您必须在每次调用cellForRowAtIndexPath 时设置单元格背景和cell.textLabel.backgroundColor,而不仅仅是在alloc/init 阶段(即如果tableView 有出列缓存未命中)。

    所以,代码变成了这样:

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

    【讨论】:

    • 实际上我还需要做 1 件事。 cell.textLabel.backgroundColor 需要设置AFTER 单元格的背景颜色。我不知道为什么我需要这样做,但现在它正在工作。谢谢迈克!
    【解决方案2】:

    您是否考虑过设置单元格 contentView 的背景颜色并保持单元格、附件和图像视图透明?

    另外,this article 可能有助于向您展示一些替代方案。

    【讨论】:

    • 我是从那篇文章中获得灵感的,但不幸的是他没有使用透明单元格。设置 contentView 背景颜色有很奇怪的效果。我也尝试设置 contentView.opaque = NO,但看起来仍然很糟糕。
    猜你喜欢
    • 2015-06-18
    • 1970-01-01
    • 1970-01-01
    • 2010-12-02
    • 2010-11-03
    • 1970-01-01
    • 2011-03-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多