［iOS］Storyboardを使わずに、セクション分けされたテーブルを実装する方法

<img width="375" alt="IMG_1861.PNG" src="https://qiita-image-store.s3.amazonaws.com/0/199441/a6c7dc46-7e79-d5bf-504d-4f450c796457.png">


## 共有すること
Storyboardを使わずに、セクション分けされたテーブルを実装する方法

## 参考資料
[《逆引きSwift》セクション分けしたUITableViewを作る] (http://docs.fabo.io/swift/uikit/019_sectionuitableview.html)

##　コード
```swift
import UIKit

//コードでテーブルを実装するためには、ViewControllerに２つのプロトコル（UITableViewDataSourceおよびUITableViewDelegate）を批准させる。

class ViewController: UIViewController,UITableViewDataSource,UITableViewDelegate {
    //セクションとして北海道の振興局を設定
    var hokkaido = ["石狩","空知","胆振","後志","檜山","渡島","上川","留萌","宗谷","日高","十勝","根室","釧路","オホーツク"]
    //各セクションに代入するデータ（振興局別の都市名）を設定
    var ishikari = ["札幌","江別","千歳","恵庭","北広島","石狩"]
    var sorachi = ["岩見沢","夕張","美唄","芦別","赤平","三笠","滝川","砂川","歌志内","深川"]
    var iburi = ["室蘭","苫小牧","登別","伊達",]
    var shiribeshi = ["小樽","倶知安"]
    var hiyama = ["江差"]
    var oshima = ["函館","北斗"]
    var kamikawa = ["旭川","富良野","士別","名寄"]
    var rumoi = ["留萌"]
    var soya = ["稚内"]
    var hidaka = ["日高"]
    var tokachi = ["帯広"]
    var nemuro = ["根室"]
    var kushiro = ["釧路"]
    var okhotsk = ["北見","網走","紋別"]


    override func viewDidLoad() {
        super.viewDidLoad()
        //ステータスバーの高さを取得
        let statusbarHeight: CGFloat = UIApplication.shared.statusBarFrame.size.height
        //ディスプレイの幅・高さを取得
        let displayWidth: CGFloat = self.view.frame.width
        let displayHeight: CGFloat = self.view.frame.height
        //テーブルビューの大きさ・位置を設定
        let sampleTableView: UITableView = UITableView(frame: CGRect(x: 0, y: statusbarHeight, width: displayWidth, height: displayHeight))
        //Identifierを"sampleCell"に設定
        sampleTableView.register(UITableViewCell.self, forCellReuseIdentifier: "sampleCell")
        sampleTableView.dataSource = self
        sampleTableView.delegate = self
        self.view.addSubview(sampleTableView)
    }
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return hokkaido.count
    }
    
    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        return hokkaido[section] as? String
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        switch section {
        case 0:
            return ishikari.count
        case 1:
            return sorachi.count
        case 2:
            return iburi.count
        case 3:
            return shiribeshi.count
        case 4:
            return hiyama.count
        case 5:
            return oshima.count
        case 6:
            return kamikawa.count
        case 7:
            return rumoi.count
        case 8:
            return soya.count
        case 9:
            return hidaka.count
        case 10:
            return tokachi.count
        case 11:
            return nemuro.count
        case 12:
            return kushiro.count
        default:
            return okhotsk.count
        }
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "sampleCell", for: indexPath)
        
        switch indexPath.section {
        case 0:
            cell.textLabel?.text =  "\(ishikari[indexPath.row])"
        case 1:
            cell.textLabel?.text =  "\(sorachi[indexPath.row])"
        case 2:
            cell.textLabel?.text =  "\(iburi[indexPath.row])"
        case 3:
            cell.textLabel?.text =  "\(shiribeshi[indexPath.row])"
        case 4:
            cell.textLabel?.text =  "\(hiyama[indexPath.row])"
        case 5:
            cell.textLabel?.text =  "\(oshima[indexPath.row])"
        case 6:
            cell.textLabel?.text =  "\(kamikawa[indexPath.row])"
        case 7:
            cell.textLabel?.text =  "\(rumoi[indexPath.row])"
        case 8:
            cell.textLabel?.text =  "\(soya[indexPath.row])"
        case 9:
            cell.textLabel?.text =  "\(hidaka[indexPath.row])"
        case 10:
            cell.textLabel?.text =  "\(tokachi[indexPath.row])"
        case 11:
            cell.textLabel?.text =  "\(nemuro[indexPath.row])"
        case 12:
            cell.textLabel?.text =  "\(kushiro[indexPath.row])"
        default:
            cell.textLabel?.text =  "\(okhotsk[indexPath.row])"
        }
        
        return cell
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        
    }

}

```