LoginSignup
0
3

More than 5 years have passed since last update.

Eurekaを使わずに簡単に設定画面を作る | iOS

Posted at

on.gif

環境

  • swift3
  • xcode8
  • UserDefaults

説明

Eurekaを使わないでiOSの設定画面をswiftを用いて作っていきます。UserDefaultsを使って永続化処理も実装しています。

ソース

GitHub

実装

スクリーンショット 2017-07-23 10.19.23.png



import UIKit

var testToggle = "testToggleOn"
var testToggle02 = "testToggle02On"

//配列の実装
var TableTitle = [ ["MainTitle01", "Title01", "Title02", "Title03", "Title04"],
                   ["MainTitle02", "Title05", "Title06", "Title07"],
                   ["MainTitle03", "Title08"]
                 ]
var TableSubtitle = [ ["", UserDefaults.standard.string(forKey: SettingList.appleValue), "subtitle03", "subtitle04", "subtitle05"],
                      ["","subtitle05","subtitle06" , "subtitle07"],
                      ["", "subtitle08"],
                 ]

//永続化実装
enum SettingList: CustomStringConvertible {

    case testToggleCase
    //未使用
    case testToggleCase02
    static var appleValue: String = testToggle
    static var testToggleCase02Value: String = testToggle02
    var description: String {
        switch self {
        case .testToggleCase:
            return SettingList.appleValue
        case .testToggleCase02:
            return SettingList.testToggleCase02Value
        }
    }
}

//クラス
class SettingViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    @IBOutlet weak var tableView: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()

        //ユーザーデフォルト読み込み
        if (UserDefaults.standard.string(forKey: SettingList.appleValue) != nil) {
            print("データ有り")
            testToggle = UserDefaults.standard.string(forKey: SettingList.appleValue)!
        }else {
            print("データなし")
            UserDefaults.standard.set(testToggle, forKey: SettingList.appleValue)
        }
    }


    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) {
     tableView.deselectRow(at: indexPath, animated: true)

        if indexPath.section == 0 {
            if(indexPath.row == 0){

                testToggle = (testToggle == "testToggleOff") ? "testToggleOn" : "testToggleOff"
                UserDefaults.standard.set(testToggle, forKey: SettingList.appleValue)

                //読み込み
                print( UserDefaults.standard.string(forKey: SettingList.appleValue)! )
                print( testToggle )

                TableSubtitle[0][1] = UserDefaults.standard.string(forKey: SettingList.appleValue)!

                //テーブル更新
                DispatchQueue.main.async{
                    self.tableView.reloadData()
                }
            }
        }
        print("タップされたセクションの中のセルのindex番号: \(indexPath.row)")
        print("タップされたセクションのindex番号: \(indexPath.section)")
    }
}



参考

Eurekaを使わずに簡単に設定画面を作る | iOS

0
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
3