Change UITable section backgroundColor without loosing section Title

前端 未结 5 2163
北海茫月
北海茫月 2020-12-15 00:57

i have changed the backgroundColor/backgroundImage of my tableviews sections.
I am using plain tableView. No worries, get worked without problems with this code:

5条回答
  •  别那么骄傲
    2020-12-15 01:59

    Implement method below from UITableViewDelegate, and needed ui view.

    func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let view = UITableViewHeaderFooterView()
        view.contentView.backgroundColor = UIColor(white: 0.97, alpha: 1)
        return view
    }
    

    If you need change footer use similar code in ...viewForFooterInSection.. method

    Want to draw you own font, use similar customization:

    ...    
    let label = UILabel(frame: CGRectMake(0, 0, CGRectGetWidth(tableView.bounds), tableView.sectionHeaderHeight))
    label.font = UIFont(name: "HelveticaNeue", size: 14)!
    label.text = self.tableView(tableView, titleForHeaderInSection: section)
    view.contentView.addSubview(label)
    
    view.textLabel.hidden = true
    ...
    

提交回复
热议问题