【问题标题】:UITableView set background colorUITableView 设置背景颜色
【发布时间】:2014-09-04 18:17:26
【问题描述】:

我在 tableView:cellForRowAtIndexPath 方法中更改 UITableViewCells 的背景颜色

    if(indexPath.row % 2 == 0){
        cell.backgroundColor = ...
    } else{
        cell.backgroundColor = ...
    }

但这只会改变tableView:numberOfRowsInSection中指定的单元格数量的颜色(如附图所示,前四个之后有白色单元格)

是否可以更改屏幕上显示的所有单元格的颜色?

【问题讨论】:

  • 但是你为什么显示空行??
  • 我认为这是 tableview 显示与屏幕高度一样多的行的默认行为?如果只向用户显示几个数据集,那看起来有点奇怪,所以空的数据集是白色的。
  • 如果您添加一个空的页脚视图,那么多余的行将消失,并为其余行提供一种纯色。

标签: ios objective-c uitableview swift


【解决方案1】:
  1. 打开故事板
  2. 选择你的 UITableView
  3. 打开属性检查器
  4. 滚动到查看组
  5. 为整个表格选择背景颜色。

【讨论】:

  • 谢谢,现在看起来好多了。但是我是否也可以实现与表格相同的样式(奇数和偶数单元格的颜色不同)还是默认情况下不可能?
  • 那么像提供一个自定义视图作为 UITableView 的背景视图?
  • 要管理 tableview 的背景,您可以使用 self.tableView.backgroundView.layer。这是一个很好的教程:raywenderlich.com/2502/…
  • 谢谢,我最终使用了 user3386109 建议的解决方案。但是 CALayers 真的很有趣。他们帮助我解决了我遇到的另一个问题(在 TableView 中的部分标题中添加阴影)。
  • 这对我不起作用。根据 user3386109 在 cellForRowAtIndexPath 中逐个单元格地执行此操作。也许表格被它的所有单元格或类似的东西覆盖,所以我从来没有看到实际的表格背景。不知道这是什么情况
【解决方案2】:

如果您希望单元格背景颜色继续交替,那么您需要谎报表格中有多少行。具体来说,在 tableView:numberOfRowsInSection 中,您需要始终返回一个将填满屏幕的数字,而在 tableView:cellForRowAtIndexPath 中,为超出末尾的行返回一个空白单元格的表。以下代码演示了如何执行此操作,假设 self.dataArray 是 NSString 的 NSArray。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if ( self.dataArray.count < 10 )
        return( 10 );
    else
        return( self.dataArray.count );
}

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

    if ( indexPath.row % 2 == 0 )
        cell.backgroundColor = [UIColor orangeColor];
    else
        cell.backgroundColor = [UIColor redColor];

    if ( indexPath.row < self.dataArray.count )
        cell.textLabel.text = self.dataArray[indexPath.row];
    else
        cell.textLabel.text = nil;

    return cell;
}

【讨论】:

    【解决方案3】:

    试试这样:-

    self.tableView.backgroundView.backgroundColor = [UIColor blueColor];
    

    【讨论】:

      【解决方案4】:

      在swift中,您可以更改tableview背景颜色,也可以像这样将图像设置为tableview背景颜色;

      override func viewDidLoad() {
          super.viewDidLoad()
          self.navigationController?.view.backgroundColor = UIColor(patternImage: UIImage(named: "background.png")!)
          self.tableView.backgroundColor = UIColor.clearColor()
      }
      
      
      //    change cell text color and background color
      override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
              cell.backgroundColor = UIColor.clearColor()
      }
      

      【讨论】:

        【解决方案5】:

        我用它为表格视图中的备用单元格着色

        func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell,
            forRowAtIndexPath indexPath: NSIndexPath) {
        
                if (indexPath.row % 2 == 0)
                {
                    cell.backgroundColor = UIColor.grayColor()
                }
                else
                {
                    cell.backgroundColor = UIColor.whiteColor()
                }
        }
        

        【讨论】:

          【解决方案6】:

          您需要将 tableview 的 backgroundView 设置为 nil 并将其 backgroundColor 设置为所需的颜色。

          【讨论】:

            【解决方案7】:

            对于 SWIFT

            感谢@Irshad Qureshi,通过在我的 cellForRowAtIndexPath 中添加以下内容,我能够获得原型单元格的交替背景颜色:

            if (indexPath.row % 2 == 0)
                {
                    cell!.backgroundColor = UIColor.groupTableViewBackgroundColor()
                }
                else
                {
                    cell!.backgroundColor = UIColor.whiteColor()
                }
            

            【讨论】:

            • 欢迎兄弟
            【解决方案8】:

            Swift 5 及更高版本

             override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
                //Set Clear Backcolor
                //cell.backgroundColor = .clear
                //or in your case
                if ( indexPath.row % 2 == 0 )
                    cell.backgroundColor = .orange
                else
                    cell.backgroundColor = .red
            }
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2011-10-07
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2023-03-22
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多