【问题标题】:Problem with transparent text background in UITableViewUITableView中的透明文本背景问题
【发布时间】:2009-12-05 15:48:31
【问题描述】:

我的表格视图中有一个烦人的问题。我能够获得颜色变化的单元格(蓝色/白色/蓝色/...),但现在我的文本遇到了问题,蓝色单元格上有白色背景。

我已尝试将背景颜色设置为红色:

// try to set the backgroundcolor of the text ???
cell.textLabel.text.backgroundColor = [UIColor redColor];

这不起作用;哼。

请看我下面的代码;谁能告诉我出了什么问题以及如何通过为文本提供透明背景来解决我的问题?


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView 
             cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
    }

    // Set up the cell
    cell.textLabel.text=[RssFeedNodes objectAtIndex:indexPath.row];

    // try to set the backgroundcolor of the text ???
    cell.textLabel.text.backgroundColor = [UIColor redColor];


    // show image in cell
    NSString *imageName=@"rss.png";
    cell.imageView.image=[UIImage imageNamed:imageName];

    // changing colors in cells
    NSInteger row = [indexPath row];
    if (row % 2){ 
        cell.contentView.backgroundColor = [UIColor whiteColor];
    }
    else { 
        cell.contentView.backgroundColor = [UIColor colorWithRed:0.90f green:0.95f blue:1.0f alpha:1.0f];
    }

    return cell;
}

【问题讨论】:

    标签: iphone uitableview iphone-sdk-3.0 background


    【解决方案1】:

    你的答案在这里找到(并且描述得很好):http://undefinedvalue.com/2009/11/02/easy-gradient-backgrounds-uitextviewcells

    我对解决方案的简要总结:子类化 UITableViewCell,然后在 cellForRowAtIndexPath 中创建实例时使用您的子类。

    然后你只需要在你的子类 UITableViewCell 中重写一个方法(setSelected):

    • (void)setSelected:(BOOL)选定动画:(BOOL)动画{

      [super setSelected:selected动画:animated];

      // 为选中状态配置视图 for (UIView *view in self.contentView.subviews) { view.backgroundColor = [UIColor clearColor]; } }

    原因似乎是内置的 UITableViewCell 类在 setSelected 方法中根据表格中的选择状态显示时将标签背景设置为白色(或选定颜色)。替换您自己的,调用基类实现,然后将您的子视图背景设置为清除,以便让您的 contentView 背景发光。

    【讨论】:

      【解决方案2】:

      text 没有背景,但 textLabel 有。所以

      [[cell textLabel] setBackground:[UIColor redColor]];
      

      【讨论】:

      • 谢谢,我已经尝试过了,类似于您的解决方案: cell.textLabel.backgroundColor = [UIColor redColor];你的建议:[[cell textLabel] setBackgroundColor:[UIColor redColor]];在我的代码中不起作用,我的文本标签也没有背景颜色:-(
      【解决方案3】:

      您需要将自己的UILabel 标签添加到单元格上,并将其背景颜色设置为透明。由于某种原因,表格单元格的标签没有可设置的背景颜色。

      类似:

      UILabel* label = [[UILabel alloc] init];
      label.frame = CGRectMake( 20, 10, 200, 22 );
      label.backgroundColor = [UIColor clearColor];
      label.opaque = NO;        
      label.text = @"your text here:";
      [cell addSubview:label];
      [label release];
      

      在这个例子中,我相当随意地设置了标签的框架(实际上,这不是根据我自己的一些真实代码修改的)。您可能需要更加动态地调整大小,在这种情况下,您可能需要对单元格进行子类化并覆盖 setFrame 以保持标签的框架同步。但是硬编码的值现在应该可以帮助您。

      【讨论】:

      • thanx phil,你能告诉我如何在我的例子中做到这一点吗?我在目标 c 中有相当初学者的困难
      • 一旦你使用了自己的 UITextField,你可能只需将其背景颜色设置为 [UIColor clearColor](即,根本没有颜色)
      • 添加了示例。我也意识到,在我之前的匆忙中,我提到了 UITextField,当我应该谈论一个 UILabel
      • 嘿 phil、morion 和 wkw,非常感谢您的帮助,现在它适用于 Uilabel :-)
      • michbeck - 很高兴它有帮助。在这种情况下,您对有用的回复进行投票是有礼貌的:-)
      【解决方案4】:
      #define LABEL_TAG 99
          // whatever your label rect size should be... change as appropriate
          UIlabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 300.0, 30.0)]; 
          label.tag = LABEL_TAG;
          // set up alignment, font, autoresizemask, etc.
          label.backgroundColor = [UIColor clearColor];
          label.opaque = NO;
          [cell.contentView addSubview:label];
          [label release];
      

      需要注意的是,您不会多次将自己的标签添加到可重复使用的表格单元格中。通过设置已知的 view.tag 属性,您可以获得(或发现存在) UILabel 视图。 [cell viewWithTag:LABEL_TAG]

      每当您将可重复使用的单元格出列时,首先获取对标签视图的引用,然后执行您在使用 UITableCell 的 textLabel 时通常会执行的操作。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-06-09
        • 1970-01-01
        • 1970-01-01
        • 2010-11-22
        • 2016-12-02
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多