uitableviewcell背景颜色变得清晰

时间:2011-08-06 21:47:15

标签: ios uitableview

我的细胞背景颜色总是变得清晰。我将uiview背景颜色设置为灰色,tableview背景颜色以清除颜色,我没有设置tableviewcell背景颜色来清除颜色。但细胞背景总是显示为灰色。任何人都可以对此有所了解。

由于

enter image description here

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

3 个答案:

答案 0 :(得分:5)

尝试在方法

中设置单元格的背景颜色
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath

而不是cellForRowAtIndexPath:方法。

答案 1 :(得分:4)

你的细胞不完全透明。设置UITableView的backgroundColor做了一些疯狂的未记录的东西。最好的方法是将它设置为某种半透明的颜色,例如[UIColor colorWithWhite:0.5 alpha:0.5],你会得到这样的颜色:

weird table view

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

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

答案 2 :(得分:0)

我不明白你的问题。您将表格的背景设置为灰色,然后清除,但是您没有将其设置为清晰,然后它显示为灰色,即使您将其设置为灰色也不想要它?

table.backgroundColor = [UIColor clearColor];