如何设置tableView的背景颜色

Pra*_*een 2 iphone xcode uitableview uiviewcontroller

我一直尝试但无法成功设置TableView的backgroundColor.设置tableView.backgroundColor和/或cell.backgroundColorclearColor当父视图控制器是没有工作UIViewContoller.

我的nib文件结构是

FileOwner
View
UITableView
Run Code Online (Sandbox Code Playgroud)

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

第一次尝试,我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];
Run Code Online (Sandbox Code Playgroud)

它有效,但它隐藏了细胞的内容.我能够看到标题的内容,但不能看到单元格内容.(但是当我更改视图的坐标(0,150,160,300)时,我能够看到单元格的内容,但随后它会松开tableview的backgroundColor.

第二次尝试,我创建了imageView

View
ImageView
UITableView
Run Code Online (Sandbox Code Playgroud)

并设置self.tableView.backgroundColor = [UIColor clearColor];但没有奏效.

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

dra*_*ard 5

具有分组样式的UITableView使用背景视图.要删除背景视图并显示背景颜色,请尝试以下任一操作:

tableView.backgroundView = nil;
tableView.backgroundView = [[[UIView alloc] init] autorelease];
Run Code Online (Sandbox Code Playgroud)

之后,您应该能够正常设置背景颜色:

tableView.backgroundColor = [UIColor redColor];
Run Code Online (Sandbox Code Playgroud)

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