How to set the UITableView Section title programmatically (iPhone/iPad)?

前端 未结 8 2097
太阳男子
太阳男子 2020-12-07 10:54

I\'ve created a UITableView in Interface Builder using storyboards. The UITableView is setup with static cells and a numb

8条回答
  •  北海茫月
    2020-12-07 11:34

    I don't know about past versions of UITableView protocols, but as of iOS 9, func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? is part of the UITableViewDataSource protocol.

       class ViewController: UIViewController {
    
          @IBOutlet weak var tableView: UITableView!
    
          override func viewDidLoad() {
             super.viewDidLoad()
             tableView.dataSource = self
          }
       }
    
       extension ViewController: UITableViewDataSource {
          func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
             return "Section name"
          }
       }
    

    You don't need to declare the delegate to fill your table with data.

提交回复
热议问题