2015-10-14 153 views
0

我創建了一個自定義的UITableViewHeaderFooterView,並且tableview的背景顏色是白色的。自定義UITableView標題設置背景顏色失敗

self.contentView.backgroundColor = UIColor.clearColor() 

但是,節標題的背景總是顯示爲灰色。我怎樣才能刪除灰色背景?

enter image description here

因爲我有重載的UITableView的FUNC的drawRect,所以我想要的東西出現在標題視圖後面。

我曾嘗試以下:

a)改變的UITableView風格分組,問題消失,但頭不能粘在桌子上。

b)中使用節標題的標題,而不是頭視圖

func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? 

頭的背景是透明的,但是我有多個標籤。

任何人都可以幫我解決這個問題嗎?

由於@Omkar,正確的方法是設置

cell.layer.backgroundColor = UIColor.clearColor().CGColor 
+0

你有沒有加入充當頁眉/頁腳視圖自定義單元格?如果是,則將自定義單元格的背景顏色設置爲清除顏色。你有沒有重寫viewForHeaderInSection方法? –

+0

@OmkarGuhilot是的,我已經覆蓋viewForHeaderInSection方法,並創建一個自定義UITableViewHeaderFooterView,我也設置了背景顏色清除。這是行不通的。但將內容視圖的背景顏色更改爲除明確之外的其他顏色作品。 –

+0

您是否設置了用作頁眉/頁腳視圖的自定義單元格的背景顏色以清除顏色? –

回答

3

您需要設置的內容視圖的背景色清除顏色,並在同一時間,你還需要設置的背景顏色tableView單元清除顏色。將其放置在viewForHeaderInSection方法中。您將能夠看到設置到tableView的顏色。

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

請在代碼中找到我的代碼以及其樣式爲純風格的表視圖的附加圖像。而且我也加入了它的外觀圖像上滾動

enter image description here

enter image description here

enter image description here

感謝

+0

@Joe SHI你是否能夠實現它?因爲我已經實現了相同的代碼,它的工作原理 –

+0

不,我已經改變了表格單元格的背景和內容視圖的背景以清除,否則我將無法看到彩色圓點下的行。你確定你正在使用UITableViewPlain風格。如果我更改爲UITableViewGrouped,它可以工作,但標題不粘在頂部 –

+0

是的,我使用的是UItableViewPlain風格。你想讓我附上代碼嗎?爲此,我將不得不再次發佈答案 –

0

這是我的ViewController代碼後,唯一的區別是我使用headerfootview子類。我將桌面視圖的背景設置爲藍色,如果我拉下一點,你會看到在標題上看起來像一個面具。

enter image description here

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 
} 

}

+0

@omkar Guhilot,如果我滾動到底部並向後滾動。面具消失。 –