[英]Custom UITableView Header set background colour fail
我創建了一個自定義UITableViewHeaderFooterView,並且tableview的背景色是白色。
self.contentView.backgroundColor = UIColor.clearColor()
但是,節標題的背景始終顯示為灰色。 如何刪除灰色背景?
由於我已經重寫了UITableView的drawRect函數,因此我希望標題視圖后面出現一些內容。
我嘗試了以下方法:
a)將UITableView樣式更改為Grouped,問題解決了,但是頁眉無法粘貼在表格頂部。
b)使用節標題而不是標題視圖
func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String?
標頭的背景是透明的,但是我有多個標簽。
誰能幫我解決這個問題?
感謝@Omkar,設置了正確的方法
cell.layer.backgroundColor = UIColor.clearColor().CGColor
這是我的ViewController代碼,唯一的區別是我使用headerfootview子類。 我將tableview的背景設置為藍色,如果我稍稍下拉一下,您會看到標題上的蒙版看起來像個面具。
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
tableView.dataSource = self
tableView.delegate = self
tableView.registerClass(UITableViewHeaderFooterView.self, forHeaderFooterViewReuseIdentifier: "HeaderCell")
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 4
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 10
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("CustomCell", forIndexPath: indexPath)
cell.textLabel?.text = "\(indexPath.row)"
return cell
}
func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let cell = tableView.dequeueReusableHeaderFooterViewWithIdentifier("HeaderCell") as UITableViewHeaderFooterView!
cell!.textLabel?.text = "Header"
cell!.contentView.backgroundColor = UIColor.clearColor()
cell.backgroundColor = UIColor.clearColor()
return cell
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.