2010-05-12 278 views
1

我已經嘗試了所有的方式,但無法成功設置TableView的backgroundColor。當父視圖控制器爲UIViewContoller時, 設置爲tableView.backgroundColor和/或cell.backgroundColorclearColor不起作用。如何設置tableView的背景顏色

我的筆尖文件結構

FileOwner 
View 
UITableView 

(注:我設置的TableView到groupedTable節)

第一次嘗試,我在代碼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]; 

它創造了UIView工程,但它隱藏細胞的內容。我能夠看到標題的內容,但不能看到單元格內容。 (但是,當我改變視角(0150160300的座標),那麼我能夠看到單元格的內容,但隨後失去的tableview的backgroundColor。

第二次嘗試, 我創建了ImageView的

View 
ImageView 
UITableView 

並設置self.tableView.backgroundColor = [UIColor clearColor];但沒有奏效

我用Google搜索,但沒有和平答案

+1

是否有這個問題的答案? – Caipivara 2014-12-19 22:22:10

回答

3

這一直爲我工作:。

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

確保tableView不是零,不隱藏,具有正確的框架,並且它已被添加到正確的父視圖。

5

具有分組樣式的UITableView使用背景視圖。爲了擺脫背景視圖,並讓背景顏色顯示,請嘗試以下的:

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

之後,你應該能夠設置正常的背景色:

tableView.backgroundColor = [UIColor redColor]; 

BTW,只有一個視圖可以是另一個視圖的父視圖/超視圖,而視圖控制器不是視圖。

+0

謝謝提出,它的工作原理。它爲我提供了幾個小時的工具並搜索了很多。 – Praveen 2010-05-14 07:37:53

+1

'self.tableview.backgroundView = nil'在6.0.x中爲我工作 – Intentss 2012-12-04 07:14:51

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

斯威夫特版本

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