UITableView背景颜色

fes*_*fes 15 uitableview ipad

我已经在UITableView for ios 4.2上看过各种关于背景颜色的帖子,但我似乎无法改变背景颜色.

UIView *bgView = [[[UIView alloc] init] autorelease];
bgView.backgroundColor = [UIColor blackColor];
bgView.opaque = YES;
self.myTableView.backgroundView = bgView;
Run Code Online (Sandbox Code Playgroud)

我已将此代码放在viewDidLoad中,但它不会更改颜色.我的UITableView是UIViewController里面的一个IBOutlet,带有UITableViewDelegate和UITableViewDataSource,所以我想在viewDidLoad中UITableView已经渲染,我无法改变它的背景颜色.

编辑* - UITableView被分组

Edw*_*ynh 37

尝试将表视图的背景视图设置为nil,并设置表视图的背景颜色

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.myTableView.backgroundView = nil;
    self.myTableView.backgroundColor = [UIColor blackColor];
}
Run Code Online (Sandbox Code Playgroud)

  • 注意self.myTableView.backgroundView = nil; 在iOS 5+中让背景在iPad上运行至关重要 (6认同)

Mic*_* K. 19

只需更改tableView和tableView的backgroundView的backgroundColor属性即可.

- (void)viewDidLoad {
    [super viewDidLoad];

   id desiredColor = [UIColor clearColor];
   self.tableView.backgroundColor = desiredColor;
   self.tableView.backgroundView.backgroundColor = desiredColor;
}
Run Code Online (Sandbox Code Playgroud)


Ara*_*han 0

使用此委托来更改颜色

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    cell.backgroundColor = [UIColor redColor];
}
Run Code Online (Sandbox Code Playgroud)

编辑1:

UIView* backgroundView = [ [ [ UIView alloc ] initWithFrame:CGRectZero ] autorelease ];
backgroundView.backgroundColor = [ UIColor yellowColor ];
cell.backgroundView = backgroundView;
for ( UIView* view in cell.contentView.subviews ) 
{
    view.backgroundColor = [ UIColor clearColor ];
}
Run Code Online (Sandbox Code Playgroud)