【Swift】TableViewのHeaderSectionとFooterSection

完成図

 

f:id:nekokichi_yos2:20181112003503p:plain

 

ソースコード

 

 

import UIKit

class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
    
    @IBOutlet weak var tableView: UITableView!
    
    //セルに表示するテキスト
    var text  = ["fafeaw","faefa","gwafawfew"]
    //セクションに表示するテキスト
    var sectiontext = ["1","2","3","4"]
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        tableView.delegate = self
        tableView.dataSource = self
    }
    
    //セクションの数
    func numberOfSections(in tableView: UITableView) -> Int {
        return 3
    }
    
    //ヘッダーセクションのタイトル
    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        return sectiontext[section]
    }
    
    //ヘッダーセクションの高さ
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 60
    }
    
    //フッターセクションのタイトル
    func tableView(_ tableView: UITableView, titleForFooterInSection section: Int) -> String? {
        return "終わり"
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return text.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
        
        cell1.textLabel?.text 
        
        return cell
    }

}