簡體   English   中英

自定義UITableView標頭設置背景顏色失敗

[英]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

您需要將內容視圖的背景色設置為透明色,同時,還需要將tableView單元格的背景色設置為透明色。 將此放置在您的viewForHeaderInSection方法中。 然后,您將能夠看到為tableView設置的顏色。

    YourCell.contentView.backgroundColor = UIColor.clearColor()
    YourCell.backgroundColor = UIColor.clearColor()

請在我的代碼中找到隨附的圖像,並在情節提要中找到樣式為純樣式的表格視圖。 而且我還添加了運行后的外觀圖像

在此處輸入圖片說明

在此處輸入圖片說明

在此處輸入圖片說明

謝謝

這是我的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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM