JY-CONTENTS

JY

JY-CONTENTS
search

+

MENU

【Swift/ios】UITableViewクラス

【Swift/ios】UITableViewクラス

(DATE)

-

2018.01.15

(CATEGORY)

-

import UIKit

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
    let sectionTitle = ["肉料理","魚料理","野菜料理"]
    static let section0 = [("ステーキ","牛肉"),("鶏唐揚げ","鶏肉")]
    static let section1 = [("アジフライ","アジ"),("鯖の味噌煮","鯖"),("サーモンの刺身","鮭")]
    static let section2 = [("おひたし","ほうれん草"),("たくあん","大根"),("ゴーヤチャンプル","ゴーヤ")]
    let tableData = [section0, section1, section2]

    override func viewDidLoad() {
        super.viewDidLoad()
        
        let mytableview = UITableView(frame: view.frame, style: .grouped)
        mytableview.delegate = self
        mytableview.dataSource = self
        
        view.addSubview(mytableview)
    }
    
    
    //---------------------------------------
      //データソースメソッド(データに関する処理)
    //----------------------------------------
    //セクション数を返す
    func numberOfSections(in tableView: UITableView) -> Int{
        return tableData.count
    }
    
    //各セクションの行数(セル数)を返す(必須メソッド)
    //第2引数(section):numberOfSections(in tableView:)で返されるセクション数
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        let sectionData = tableData[section]
        return sectionData.count
    }
    
    //セクションタイトルを返す
    //第2引数(section):numberOfSections(in tableView:)で返されるセクション数
    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String?{
        return sectionTitle[section]
    }
    
    //各行に表示するセルを返す(必須メソッド)
    //第2引数(indexPath):セクション数とそのセクションの行数(セル数)の組み合わせ。
    //プロパティはsection(セクションのインデックス)とrow(セルのインデックス)
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        //セルの作成
        let cell = UITableViewCell(style: .subtitle, reuseIdentifier: "cell")
        //各セクションのデータ
        let sectionData = tableData[indexPath.section]
        //各セクションのセルに表示させるデータ
        let cellData = sectionData[indexPath.row]
        
        cell.textLabel?.text = cellData.0
        cell.detailTextLabel?.text = cellData.1
        
        return cell
    }
    
    
    //---------------------------------------
      //デリゲートメソッド(イベントに関する処理)
    //---------------------------------------
    
    //選択された行(セル)を取得
    //第2引数(indexPath):選択された行のインデックス番号
    //indexPath.sectionで選択されたセクションの番号を取得
    //indexPath.rowで選択された行番号(セル)を取得
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        //選択されたセクションのタイトル
        let title = sectionTitle[indexPath.section]
        //選択されたセクションの内容
        let sectionData = tableData[indexPath.section]
        //選択されたセルの内容
        let cellData = sectionData[indexPath.row]
        
        print("\(title), \(cellData.0), \(cellData.1)")
    }

    

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}

NEW TOPICS

/ ニュー & アップデート

SEE ALSO

/ 似た記事を見る

JY CONTENTS UNIQUE BLOG

search-menu search-menu