2015-06-06 136 views

回答

42

首先設置的tableView的背景色中viewDidLoad象下面這樣:

override func viewDidLoad() { 
    super.viewDidLoad() 
    self.tableView.backgroundColor = UIColor.lightGrayColor() 
} 

現在添加此方法:

override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) { 
    cell.backgroundColor = UIColor.clearColor() 
} 

夫特3,使用下面如果你想達到的是通過故事板,然後在選擇你的tableView的「表格視圖單元格」,「內容視圖」,然後

override func viewDidLoad() { 
    super.viewDidLoad() 
    self.tableView.backgroundColor = UIColor.lightGray 
} 

override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) { 
    cell.backgroundColor = UIColor.clear 
} 
+2

太好了!謝謝!它的工作,不知道爲什麼它不能與Storyboard一起工作..我只是在那裏試圖...感謝您的幫助.. –

+0

@Homam我有一個靜態tableview,但它不適用於我。 – Nicholas

+0

@Nicholas在viewDidLoad()方法中加入這樣的語句:self.tableView.delegate = self – Homam

8
func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) { 
    cell.contentView.backgroundColor = UIColor.yellowColor() 
} 

斯威夫特3

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) { 
    cell.contentView.backgroundColor = UIColor.yellow 
} 
4

:方法而不是一個以上屬性檢查器去查看和更改它的顏色像這樣:

Xcode

1

使用此代碼變革的tableView顏色

override func viewDidLoad() { 
     super.viewDidLoad() 

     // define tableview background color 
     self.tableView.backgroundColor = UIColor.clearColor() 
} 

變革的tableView單元格顏色

cell.backgroundColor = UIColor.clearColor() 
相關問題