【问题标题】:How to change background color of UITableViewCell using my own color combination如何使用我自己的颜色组合更改 UITableViewCell 的背景颜色
【发布时间】:2011-05-14 20:55:34
【问题描述】:

实际上,我发现了一些像我一样提出问题的线程。 但是这些线程中给出的每个解决方案都不能在我的代码中使用.. 我读到的最后一个链接是 How to customize the background color of a UITableViewCell?

我的问题是我只想用我自己的颜色更改我的 tableviewcell 背景颜色。 有人可以帮我吗?

【问题讨论】:

  • 在 willDisplayCell:forRowAtIndexPath: 中显示您尝试过的代码(包括方法名称和参数的整个方法)。您使用的是 UITableViewCell 还是自定义子类?
  • -(void)tableView:(UITableView )tableView wilLDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{ UIView backgroundView = [ [ [ UIView alloc ] initWithFrame :CGRectZero ] 自动释放 ]; backgroundView.backgroundColor = [ UIColor yellowColor ]; cell.backgroundView = 背景视图; for ( UIView* view in cell.contentView.subviews ) { view.backgroundColor = [ UIColor clearColor ]; } //cell.backgroundColor = [UIColor colorWithRed:0.2 green:0.2 blue:0.5 alpha:1]; }
  • 您的代码中有一个拼写错误应该是 willDisplayCell 但在您发布的代码中是 wilLDisplayCell 请检查...
  • thx gyani....我没意识到...谢谢... :)

标签: iphone uitableview background-color


【解决方案1】:

怎么样:

[myCell setBackgroundColor:[UIColor colorWithRed:0.2 blue:0.5 green:0.2 alpha:1]];

您可以将其放置在表格视图的willDisplayCell:forRowAtIndexPath: 数据源方法中。或者你可以继承 UITableViewCell 并将其设置在你重写的初始化程序中。或者您可以从 XIB 加载单元格并使用 Interface Builder 设置颜色。

【讨论】:

  • 这必须在 willDisplayCell:forRowAtIndexPath: 中完成。它在 cellForRowAtIndexPath 中不起作用。请参阅 UITableViewCell 的文档。
  • 我是这样尝试的,抱歉没有在我的问题中声明它,但仍然没有显示背景颜色
  • 它也没有显示,即使我把它移到 willDisplayCell:forRowAtIndexPath
【解决方案2】:

试试这个..

cell.backGroundColor = [UIColor colorWithRed:190.0/255.0 blue:190.0/255.0 green:190.0/255.0 alpha:1.0];

将此代码写入 cellForRowAtIndexPath 函数。 你将简单地从 MS Paint 中获得这些不同的 RGB 组合,alpha 是介于0.01.0 之间的不透明度。

【讨论】:

    【解决方案3】:

    另一个帮助解决这个问题的链接...UITableViewCell color issues with custom table view background

    【讨论】:

      【解决方案4】:

      最后我发现了如何解决这个问题。但我选择使用图像。我在另一个问题中声明了我的解决方法:is there somebody who made an application using background for tableview and tableviewcell?

      【讨论】:

        【解决方案5】:

        iOS 7之前只需添加这个方法

        - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
        {
        
               [cell setBackgroundColor:[UIColor colorWithRed:(221.0/255.0) green:(221.0/255.0) blue:(221.0/255.0) alpha:1.0]];
        
        }
        

        现在在 iOS 7 中,如果您将此代码放在 cellForRowAtIndexPath 中,它将起作用

        [cell setBackgroundColor:[UIColor colorWithRed:(221.0/255.0) green:(221.0/255.0) blue:(221.0/255.0) alpha:1.0]];
        

        【讨论】:

          猜你喜欢
          • 2015-11-01
          • 1970-01-01
          • 2018-12-26
          • 1970-01-01
          • 2016-07-09
          • 2012-06-05
          • 1970-01-01
          • 2010-09-18
          • 2010-09-21
          相关资源
          最近更新 更多