UITableView设置背景颜色

时间:2014-07-14 18:21:03

标签: ios objective-c uitableview swift

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

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

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

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

enter image description here

8 个答案:

答案 0 :(得分:38)

  1. 打开故事板
  2. 选择您的UITableView
  3. 打开属性检查器
  4. 滚动至查看论坛
  5. 选择整个表格的背景颜色。
  6. enter image description here

答案 1 :(得分:17)

如果您希望单元格背景颜色继续交替,那么您需要说明表格中有多少行。具体来说,在 tableView:numberOfRowsInSection 中,您需要始终返回一个将填充屏幕的数字,并在 tableView:cellForRowAtIndexPath 中,为超出结尾的行返回一个空白单元格的表。以下代码演示了如何执行此操作,假设 self.dataArray 是NSStrings的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;
}

答案 2 :(得分:10)

试试这样: -

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

答案 3 :(得分:2)

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

func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell,
    forRowAtIndexPath indexPath: NSIndexPath) {

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

答案 4 :(得分:2)

在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 :(得分:1)

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

答案 6 :(得分:1)

适用于SWIFT

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

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

答案 7 :(得分:0)

快速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
}
相关问题