Swiftで設定画面を実装する方法

Eurekaを使うよりも学習コストが低く大概既存の知識で理想的な動きを実現できるのでベタがきしたほうが早いです。でもソースの量はEurekaを使ったほうが断然少なく済みますね。

実装していないTips

 

セレクトされた後にセルのハイライトを消す

 

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {


        tableView.deselectRow(at: indexPath, animated: true)

 

タップしたセルを取得

 

 

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

print("タップされたセクションの中のセルのindex番号: \(indexPath.row)")

print("タップされたセクションのindex番号: \(indexPath.section)")

 

 

説明

 

TableViewのStyleをGroupedに変更

スクリーンショット 2017-07-03 15.59.05.png

cellの名前をcellに設定

スクリーンショット 2017-07-03 15.58.49.png

このような感じになる

スクリーンショット 2017-07-03 15.59.22.png

接続状況

スクリーンショット 2017-07-03 16.02.34.png

実装

01.gif

import UIKit





var TableTitle = [ ["menuTitle01", "title01", "title02"],
                   ["menuTitle02", "title03", "title04"],

                   ["menuTitle03", "menuTitle05", "menuTitle06"],
                   ["menuTitle04", "menuTitle07"]
                 ]


var TableSubtitle = [ ["", "subtitle02", "subtitle03"],
                      ["","subtitle05", "subtitle06"],
                      ["", "subtitle06", "subtitle07"],
                      ["", "subtitle08"]
                    ]


class TableViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    @IBOutlet weak var tableView: UITableView!


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


    override var prefersStatusBarHidden: Bool {
        return true
    }


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


    func numberOfSections(in tableView: UITableView) -> Int {
        return TableTitle.count
    }


    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return TableTitle[section].count - 1
    }

     func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = UITableViewCell(style: UITableViewCellStyle.value1, reuseIdentifier: "cell")
        cell.textLabel?.text = TableTitle[indexPath.section][indexPath.row + 1]
        cell.detailTextLabel?.text = TableSubtitle[indexPath.section][indexPath.row + 1]
        return cell
    }

     func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        return TableTitle[section][0]
    }

     func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    }
}

 

藤沢瞭介(Ryosuke Hujisawa)
  • りょすけと申します。18歳からプログラミングをはじめ、今はフロントエンドでReactを書いたり、AIの勉強を頑張っています。off.tokyoでは、ハイテクやガジェット、それからプログラミングに関する情報まで、エンジニアに役立つ情報を日々発信しています!

未整理記事