简体   繁体   English

如何设置tableView的背景颜色

[英]How to set background color of tableView

I have tried all the way but could not succeed to set backgroundColor of TableView. 我一直尝试但无法成功设置TableView的backgroundColor。 setting tableView.backgroundColor and/or cell.backgroundColor to clearColor didn't work when the parent view controller was UIViewContoller . 当父视图控制器是UIViewContoller时,将tableView.backgroundColor和/或cell.backgroundColorclearColor不起作用。

My nib file structure is 我的nib文件结构是

FileOwner
View
UITableView

(Note: i set the TableView to groupedTable section) (注意:我将TableView设置为groupsTable部分)

First attempt, I created the UIView in the code viewDidLoad 首次尝试,我在代码viewDidLoad创建了UIView

UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 160, 300)] autorelease;
[view setBackgroundColor:UIColor blueColor]; // color it just to see if it is created at the right place
[self.tableView sendSubViewToBack:view];

It works but it hides the content of cell. 它有效,但它隐藏了细胞的内容。 I am able to see the content of header but not cell content. 我能够看到标题的内容,但不能看到单元格内容。 (But when i change the co-ordinate of view(0,150,160,300) then i am able to see the cell's content but then it loose the backgroundColor of tableview. (但是当我更改视图的坐标(0,150,160,300)时,我能够看到单元格的内容,但随后它会松开tableview的backgroundColor。

Second attempt, I created the imageView 第二次尝试,我创建了imageView

View
ImageView
UITableView

and set the self.tableView.backgroundColor = [UIColor clearColor]; 并设置self.tableView.backgroundColor = [UIColor clearColor]; but did not work. 但没有奏效。

I googled but did not the peaceful answer. 我用谷歌搜索,但没有和平的答案。

A UITableView with the grouped style uses a background view. 具有分组样式的UITableView使用背景视图。 To get rid of the background view and let the background color show, try either of the following: 要删除背景视图并显示背景颜色,请尝试以下任一操作:

tableView.backgroundView = nil;
tableView.backgroundView = [[[UIView alloc] init] autorelease];

After that, you should be able to set the background color normally: 之后,您应该能够正常设置背景颜色:

tableView.backgroundColor = [UIColor redColor];

BTW, only a view can be the parent/superview of another view, and a view controller is not a view. 顺便说一下,只有一个视图可以是另一个视图的父/超级视图,而视图控制器不是视图。

This has always worked for me: 这一直对我有用:

UITableView *tableView = [[UITableView alloc] initWithFrame:tableFrame style:UITableViewStylePlain];
tableView.backgroundColor = [UIColor redColor];
[parentView addSubview:tableView];

Make sure that the tableView is not nil, not hidden, has the correct frame, and that it has been added to the correct parent view. 确保tableView不是nil,不隐藏,具有正确的框架,并且已将其添加到正确的父视图中。

Swift Version Swift版本

    let backgroundView = UIView(frame: CGRect(x: 0, y: 0, width: self.tableView.bounds.size.width, height: self.tableView.bounds.size.height))
    backgroundView.backgroundColor = UIColor.redColor()
    self.tableView.backgroundView = backgroundView
UIView* backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.bounds.size.width, self.tableView.bounds.size.height)];
[backgroundView setBackgroundColor:[UIColor redColor]];
[self.tableView setBackgroundView:backgroundView];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM