UITableView设置背景颜色

Sp0*_*ght 20 objective-c uitableview ios swift

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

    if(indexPath.row % 2 == 0){
        cell.backgroundColor = ...
    } else{
        cell.backgroundColor = ...
    }
Run Code Online (Sandbox Code Playgroud)

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

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

在此输入图像描述

Kee*_*nle 41

  1. 打开故事板
  2. 选择你的UITableView
  3. 打开属性检查器
  4. 滚动到查看群组
  5. 选择整个表格的背景颜色.

在此输入图像描述


use*_*109 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;
}
Run Code Online (Sandbox Code Playgroud)


Hus*_*bir 12

试试这样: -

self.tableView.backgroundView.backgroundColor = [UIColor blueColor];
Run Code Online (Sandbox Code Playgroud)