自定义UITableView标题设置背景颜色失败
我创build了一个自定义的UITableViewHeaderFooterView,并且tableview的背景颜色是白色的。
self.contentView.backgroundColor = UIColor.clearColor()
但是,节标题的背景总是显示为灰色。 我怎样才能删除灰色背景?
因为我已经覆盖了UITableView的drawRect func,所以我想要在标题视图后面出现一些东西。
我已经尝试了以下内容:
a)将UITableView样式更改为分组,但问题依然存在,但是标题不能粘在表格顶部。
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()
请为我的代码find附加的图像,也可以在故事板中find风格为平面样式的表视图。 而且我还添加了运行后的样子
谢谢
这是我的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 }
}