自定义UITableView标题设置背景颜色失败

问题描述:

我创建了一个自定义的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

您是否设置了用作页眉/页脚视图的自定义单元格的背景颜色以清除颜色? –

您需要设置的内容视图的背景色清除颜色,并在同一时间,你还需要设置的背景颜色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风格。你想让我附上代码吗?为此,我将不得不再次发布答案 –

这是我的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,如果我滚动到底部并向后滚动。面具消失。 –