【问题标题】:uitableviewcell background color becomes clearcoloruitableviewcell 背景色变为clearcolor
【发布时间】:2011-08-06 21:47:15
【问题描述】:

我对单元格背景颜色总是变成 clearcolor 有问题。我将uiview背景颜色设置为灰色,tableview背景颜色设置为清除颜色,并且我没有将tableviewcell背景颜色设置为清除颜色。但是单元格背景总是显示为灰色。任何人都可以对此有任何想法。

谢谢

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

【问题讨论】:

    标签: ios uitableview


    【解决方案1】:

    尝试在方法中设置单元格的背景颜色

    - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
    

    而不是在您的 cellForRowAtIndexPath: 方法中。

    【讨论】:

    • 这解决了我过去遇到这个问题时的问题。
    • 这个有效,但我看到的问题是这个设置了我在 numberOfRowsInSection 中指定的单元格数量的颜色,但不是表格视图中的所有单元格。所以对我来说,其中一半是白色的,其余的是灰色的。
    • Apple 文档特别说要使用这种方法,而不是“cellForRowAtIndexPath”。
    【解决方案2】:

    您的单元格不完全透明。设置 UITableView 的 backgroundColor 会做一些疯狂的未记录的事情。最好的查看方法是将其设置为像[UIColor colorWithWhite:0.5 alpha:0.5] 这样的半透明颜色,这样你会得到这样的结果:

    要解决您的问题,您必须在设置 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;
    }
    

    【讨论】:

    • 当我使用这个代码时,人字形周围的区域仍然显示为灰色。所以我尝试使用添加 cell.accessoryview.background = cellBackgroundColor 但它仍然无法正常工作。任何想法有什么问题。我使用 UITableViewCellStyleValue2 样式。
    • 在这种情况下,您需要添加cell.detailTextLabel.backgroundColor = cellBackgroundColor;。如果还是不行,能否请您上传截图?
    • 进行您所说的更改后。我在代码中添加了图像。请看一看。这就是我的表格单元格的显示方式。
    • 啊,是的,问题是如果您设置了系统附件视图之一,它们没有设置为附件视图属性,因此更改 cell.accessoryView.backgroundColor 没有效果。您可能应该在bugreport.apple.com 上将其报告为错误...我想我知道如何解决此问题,请给我几分钟,我会再次更新我的答案。
    • 就是这样,希望它现在修复了所有的漏洞 :) 如果你想让它更快一点,你应该把这 4 行放在 if (cell == nil) { ... } 内,只分配并设置 backgoundView在新创建的单元格上。
    【解决方案3】:

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

    table.backgroundColor = [UIColor clearColor];
    

    【讨论】:

    • 我将 uiview 背景颜色设置为灰色,将 tableview 设置为清除颜色。
    猜你喜欢
    • 2016-11-25
    • 2021-08-22
    • 2019-05-16
    • 1970-01-01
    • 2012-06-05
    • 1970-01-01
    • 2015-07-20
    • 1970-01-01
    • 2011-08-05
    相关资源
    最近更新 更多