UITableViewCell透明背景(包括imageView / accessoryView) [英] UITableViewCell transparent background (including imageView/accessoryView)

查看:161
本文介绍了UITableViewCell透明背景(包括imageView / accessoryView)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

When I set the UITableViewCells backgroundColor to a semi-transparent color, it looks good, but the color doesn't cover the entire cell.

imageView accessoryView 周围的区域将以<$ c $的价格出现c> [UIColor clearColor] ...

The area around the imageView and accessoryView are coming up as [UIColor clearColor]...

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

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. 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 :这是原始表格单元格代码:

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.

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

You have to set the cell background and cell.textLabel.backgroundColor every time cellForRowAtIndexPath is called, not just during the alloc/init phase (i.e. if the tableView has a dequeue cache miss).

因此,代码变为:

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

这篇关于UITableViewCell透明背景(包括imageView / accessoryView)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆