2017-08-02 51 views
0

我有一个使用Swift3和TableView构建的应用程序。我使用故事板,tableview放置在视图控制器内。我正在通过代码使用部分和设置部分标题。我希望节标题显示与我为表视图设置的相同的灰色背景。我试图设置相同的颜色代码,但它没有帮助。所以我尝试设置为UIColor.clear,但它显示为部分标题的白色背景。我需要它使用tableview背景。你能指导我什么是错的。UIColor.Clear在swift3中使用部分标题时没有给出预期结果

代码:

func tableView(_ tableView: UITableView, willDisplayFooterView view: UIView, forSection section: Int) { 
     print("willDisplayFooterView.. for section \(section) ") 
     (view as! UITableViewHeaderFooterView).backgroundView?.backgroundColor = UIColor.clear //UIColor(red: 232.0, green: 232.0, blue: 232, alpha: 1.0) 
    } 

    func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) { 
     print("willDisplayHeaderView,... for section \(section) ") 
     (view as! UITableViewHeaderFooterView).backgroundView?.backgroundColor = UIColor.clear //UIColor(red: 232.0, green: 232.0, blue: 232, alpha: 1.0) 
    } 

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

     print("titleForHeaderInSection") 
     return self.section [section ] 
    } 

我试图用viewForHeaderInSection,但它不会被调用。

func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { 

     print("in viewForHeaderInSection") 
     let view = UIView() 
     let label = UILabel() 

     label.text = self.section [section ]    
     view.addSubview(label) 
     view.backgroundColor = UIColor.clear 

     label.translatesAutoresizingMaskIntoConstraints = false 

     let horizontallayoutContraints = NSLayoutConstraint(item: label, attribute: .centerX, relatedBy: .equal, toItem: view, attribute: .centerX, multiplier: 1, constant: 0) 
     view.addConstraint(horizontallayoutContraints) 

     let verticalLayoutContraint = NSLayoutConstraint(item: label, attribute: .centerY, relatedBy: .equal, toItem: view, attribute: .centerY, multiplier: 1, constant: 0) 
     view.addConstraint(verticalLayoutContraint) 

     return view 
    } 

回答

1

尝试设置视图的背景颜色..而不是view.backgroundView

func tableView(_ tableView: UITableView, willDisplayFooterView view: UIView, forSection section: Int) { 
     print("willDisplayFooterView.. for section \(section) ") 
     (view as! UITableViewHeaderFooterView).backgroundColor = UIColor.clear //UIColor(red: 232.0, green: 232.0, blue: 232, alpha: 1.0) 
    } 

func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) { 
     print("willDisplayHeaderView,... for section \(section) ") 
     (view as! UITableViewHeaderFooterView).backgroundColor = UIColor.clear //UIColor(red: 232.0, green: 232.0, blue: 232, alpha: 1.0) 
    } 

免责声明:我不是在迅速语法非常好的..但希望这回答你的问题。

+0

您好..它仍然不给我预期的行为。我试着按照你的建议使用,并警告说这是一个不赞成的行为,而不是使用contentView背景色。所以我尝试了使用它,但仍然没有运气。 func tableView(_ tableView:UITableView,willDisplayFooterView view:UIView,forSection section:Int){ print(“willDisplayFooterView .. for section \(section)”) (view as!UITableViewHeaderFooterView).contentView.backgroundColor = UIColor.clear } – csharpnewbie

+0

你是否重写这个方法? “func tableView(tableView:UITableView,viewForHeaderInSection section:Int) - > UIView?” –

+0

嗨..因为我在原始问题中编辑过,我尝试使用viewForHeaderInSection,但它根本没有被调用。 – csharpnewbie

1

您可以更改内容查看的背景色,而不是backgroundView

(view as! UITableViewHeaderFooterView).contentView?.backgroundColor = UIColor. groupTableViewBackground 
+0

是的,我也尝试过,根据Marcio的建议,但它没有奏效。 – csharpnewbie

+0

Marcio建议改变backgroundColor而不是contentView。你有没有尝试改变视图的contentView的backgroundColor? – cyberme

+0

是的,我尝试使用,因为他建议,并得到警告,这是一个不赞成的行为,而不是使用contentView背景颜色。所以我尝试了使用它,但仍然没有运气。 func tableView(_ tableView:UITableView,willDisplayFooterView view:UIView,forSection section:Int){print(“willDisplayFooterView .. for section(section)”)(view as!UITableViewHeaderFooterView).contentView.backgroundColor = UIColor.clear} – csharpnewbie

相关问题