How can we add image in UITableView section header using swift?

前端 未结 6 1492
盖世英雄少女心
盖世英雄少女心 2021-02-04 10:54

In my willDisplayHeaderView I have changed color of section header But I want to add an image before section title. Any help? My code for willDisplayHeaderView is



        
6条回答
  •  隐瞒了意图╮
    2021-02-04 11:26

    You could design your own header, like you would design a custom cell in a table view. Or you could just add an image like so:

    func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    
        var myCustomView: UIImageView
        var myImage: UIImage = UIImage(named: "myImageResource")
        myCustomView.image = myImage
    
        let header: UITableViewHeaderFooterView = view as UITableViewHeaderFooterView
        header.addSubview(myCustomView)
        return header
    }
    

    Then you can simply add your section title UILabel as another subview.

提交回复
热议问题