uitableviewcell 背景颜色变为 clearcolor [英] uitableviewcell background color becomes clearcolor

查看:32
本文介绍了uitableviewcell 背景颜色变为 clearcolor的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我总是遇到单元格背景颜色变得清晰的问题.我将 uiview 背景颜色设置为灰色,将 tableview 背景颜色设置为清除颜色,而我没有将 tableviewcell 背景颜色设置为清除颜色.但单元格背景始终显示为灰色.任何人都可以对此有任何想法.

I have problem with the cell background color becoming clearcolor always. I set the uiview background color to gray color, tableview background color to clear color and I did not set tableviewcell background color to clear color. But the cell background always appears grey. Can any one have any idea about this.

谢谢

-(void)viewDidLoad
{
    self.view.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"TableBackGround.png"]];

    Acc_Details_TView.backgroundColor = [UIColor clearColor];
    Acc_Details_TView.rowHeight = 40;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"TransCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    switch ([Acc_Details_SegCtrl selectedSegmentIndex]) {
        case 0:{
            if (cell == nil) {
                cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"TableCell"] autorelease];
                cell.backgroundColor =[UIColor clearColor];
                cell.textLabel.font = [UIFont boldSystemFontOfSize:14];
                cell.detailTextLabel.text = [[transcationsList objectAtIndex:([indexPath row])] valueForKey:@"Date"];
                cell.detailTextLabel.font = [UIFont systemFontOfSize:12];
            }
            NSString *titleName =[[transcationsList objectAtIndex:([indexPath row])] valueForKey:@"Title"] ;
            if ([titleName length] > 19) {
                cell.textLabel.text = [titleName substringWithRange:NSMakeRange(0, 20)];                
            }
            else{
                cell.textLabel.text = titleName;
            }

            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            UILabel * acc_Amount = [[UILabel alloc] initWithFrame:CGRectMake(220, 5, 60,10)];
            acc_Amount.textAlignment = UITextAlignmentRight;
            acc_Amount.backgroundColor = [UIColor clearColor];
            acc_Amount.text = [[transcationsList objectAtIndex:([indexPath row])] valueForKey:@"Amount"];
            acc_Amount.font = [UIFont boldSystemFontOfSize:14];
            [cell.contentView addSubview:acc_Amount];
            UILabel * balance_Amount = [[UILabel alloc] initWithFrame:CGRectMake(220, 23, 60,10)];
            balance_Amount.textAlignment = UITextAlignmentRight;
            balance_Amount.text = @"$1234.50";
            balance_Amount.backgroundColor = [UIColor clearColor];
            balance_Amount.textColor = [UIColor grayColor];
            balance_Amount.font = [UIFont systemFontOfSize:12];
            [cell.contentView addSubview:balance_Amount];
            return cell;            
        }
    }
}

推荐答案

您的单元格并不完全透明.设置 UITableView 的 backgroundColor 做了一些疯狂的未记录的事情.最好的方法是将它设置为一些半透明的颜色,比如 [UIColor colorWithWhite:0.5 alpha:0.5] ,你会得到这样的东西:

Your cells are not exactly transparent. Setting UITableView's backgroundColor does some crazy undocumented stuff. Best way to see this is to set it to some semi-transparent color like [UIColor colorWithWhite:0.5 alpha:0.5] by which you get something like this:

要解决您的问题,您必须在设置 tableView 后设置单元格的 contentView.backgroundColor 和所有子视图的 backgroundColors.这是您的 cellForRowAtIndexPath: 更新如下:

To fix your problem, you will have to set cells' contentView.backgroundColor and backgroundColors of all the subviews after setting tableView's. Here is your cellForRowAtIndexPath: updated with this:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"TransCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    UIColor *cellBackgroundColor = [UIColor whiteColor];

    switch ([Acc_Details_SegCtrl selectedSegmentIndex]) {
        case 0:{
            if (cell == nil) {
                cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"TableCell"] autorelease];
                cell.textLabel.font = [UIFont boldSystemFontOfSize:14];
                cell.detailTextLabel.text = [[transcationsList objectAtIndex:([indexPath row])] valueForKey:@"Date"];
                cell.detailTextLabel.font = [UIFont systemFontOfSize:12];
                cell.accessoryView.backgroundColor = cellBackgroundColor;
                cell.backgroundView.backgroundColor = cellBackgroundColor;
                cell.contentView.backgroundColor = cellBackgroundColor;
                cell.textLabel.backgroundColor = cellBackgroundColor;
                cell.detailTextLabel.backgroundColor = cellBackgroundColor;
                UIView *backView = [[UIView alloc] initWithFrame:cell.frame];
                backView.backgroundColor = cellBackgroundColor;
                cell.backgroundView = backView;
                [backView release];
            }
            NSString *titleName =[[transcationsList objectAtIndex:([indexPath row])] valueForKey:@"Title"] ;
            if ([titleName length] > 19) {
                cell.textLabel.text = [titleName substringWithRange:NSMakeRange(0, 20)];                
            }
            else{
                cell.textLabel.text = titleName;
            }

            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            UILabel * acc_Amount = [[UILabel alloc] initWithFrame:CGRectMake(220, 5, 60,10)];
            acc_Amount.textAlignment = UITextAlignmentRight;
            acc_Amount.backgroundColor = cellBackgroundColor;
            acc_Amount.text = [[transcationsList objectAtIndex:([indexPath row])] valueForKey:@"Amount"];
            acc_Amount.font = [UIFont boldSystemFontOfSize:14];
            [cell.contentView addSubview:acc_Amount];
            UILabel * balance_Amount = [[UILabel alloc] initWithFrame:CGRectMake(220, 23, 60,10)];
            balance_Amount.textAlignment = UITextAlignmentRight;
            balance_Amount.text = @"$1234.50";
            balance_Amount.backgroundColor = cellBackgroundColor];
            balance_Amount.textColor = [UIColor grayColor];
            balance_Amount.font = [UIFont systemFontOfSize:12];
            [cell.contentView addSubview:balance_Amount];
        }
    }
    return cell;
}

这篇关于uitableviewcell 背景颜色变为 clearcolor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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