xcode 如何设置tableView的背景色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2822152/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 19:21:27  来源:igfitidea点击:

How to set background color of tableView

iphonexcodeuitableviewuiviewcontroller

提问by Praveen

I have tried all the way but could not succeed to set backgroundColor of TableView. setting tableView.backgroundColorand/or cell.backgroundColorto clearColordidn't work when the parent view controller was UIViewContoller.

我已经尝试了所有方法,但无法成功设置 TableView 的 backgroundColor。设置tableView.backgroundColor和/或cell.backgroundColorclearColor当父视图控制器是没有工作UIViewContoller

My nib file structure is

我的 nib 文件结构是

FileOwner
View
UITableView

(Note: i set the TableView to groupedTable section)

(注意:我将 TableView 设置为 groupedTable 部分)

First attempt, I created the UIViewin the code viewDidLoad

第一次尝试,我UIView在代码中创建了viewDidLoad

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];but did not work.

并设置了self.tableView.backgroundColor = [UIColor clearColor];但没有用。

I googled but did not the peaceful answer.

我用谷歌搜索但没有和平的答案。

回答by drawnonward

A UITableView with the grouped style uses a background view. To get rid of the background view and let the background color show, try either of the following:

具有分组样式的 UITableView 使用背景视图。要摆脱背景视图并显示背景颜色,请尝试以下任一方法:

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.

顺便说一句,只有一个视图可以是另一个视图的父视图/超视图,而视图控制器不是视图。

回答by MrHen

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 不为零,不隐藏,具有正确的框架,并且已添加到正确的父视图中。

回答by Ahmed Safadi

Swift Version

迅捷版

    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

回答by barryjones

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];